自定义View之基础概念(一)

1. TouchSlop:

TouchSlop是一个常量,保存的是系统所能识别出的最小滑动距离。
获取方式:ViewConfiguration.get(this).getScaledTouchSlop();

2. VelocityTracker:

VelocityTracker,速度追踪,通过这个类我们可以获取手指在滑动时的速度,其中包括水平和垂直方向的速度
1.  在View的onTouch方法中添加如下代码:

    VelocityTracker velocityTracker = VelocityTracker.obtain();
    velocityTracker.addMovement(event);

2. 获取当前X,Y方向的滑动速度:

    velocityTracker.computeCurrentVelocity(1000);
    float xVelocity = velocityTracker.getXVelocity();
    float yVelocity = velocityTracker.getYVelocity();

3. 解除速度追踪

    private void stopVelocityTracker() {
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }

3. GestureDetector手势识别器

1.创建手势识别器
    GestureDetector gestureDetector = new GestureDetector();

2.创建识别器监听对象
private class GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener {

    //手指轻轻触摸屏幕的一瞬间触发
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    //手指轻轻触摸屏幕,尚未松开或拖动时触发
    @Override
    public void onShowPress(MotionEvent e) {

    }

    //手指轻轻触摸屏幕后松开时触发
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    //手指按下屏幕后拖动时触发
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    //长按屏幕时触发
    @Override
    public void onLongPress(MotionEvent e) {

    }

    //按下屏幕,快速滑动后松开时触发
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
    });
}

3. 重写view的onTouchEvent方法 交给手势识别器处理
@Override
public void onTouchEvent(MotionEvent event){
    return gestureDetector.onTouchEvent(event);
}

4. Configuration

Configuration用来描述设备的配置信息。
比如用户的配置信息:locale和scaling等等
比如设备的相关信息:输入模式,屏幕大小, 屏幕方向等等

Configuration configuration=getResources().getConfiguration();
//获取国家码
int countryCode=configuration.mcc;
//获取网络码
int networkCode=configuration.mnc;

5. ViewConfiguration

ViewConfiguration提供了一些自定义控件用到的标准常量,比如尺寸大小,滑动距离,敏感度等等。

ViewConfiguration  viewConfiguration=ViewConfiguration.get(context);
//获取touchSlop。该值表示系统所能识别出的被认为是滑动的最小距离
int touchSlop = viewConfiguration.getScaledTouchSlop();
//获取Fling速度的最小值和最大值
int minimumVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
int maximumVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
//判断是否有物理按键
boolean isHavePermanentMenuKey=viewConfiguration.hasPermanentMenuKey();

/双击间隔时间.在该时间内是双击,否则是单击
int doubleTapTimeout=ViewConfiguration.getDoubleTapTimeout();
//按住状态转变为长按状态需要的时间
int longPressTimeout=ViewConfiguration.getLongPressTimeout();
//重复按键的时间
int keyRepeatTimeout=ViewConfiguration.getKeyRepeatTimeout();

6. scrollTo和scrollBy

第一点:scrollTo()和scrollBy()的关系
public void scrollBy(int x, int y) {
    scrollTo(mScrollX + x, mScrollY + y);
}

scrollBy( )调用了scrollTo( ),最终起作用的是scrollTo( )方法。

第二点:scroll的本质
scrollTo( )和scrollBy( )移动的只是View的内容,而且View的背景是不移动的。

scrollBy()方法是基于当前位置的相对滑动。
scrollTo()方法是基于所传递参数的绝对滑动。

7. 联系方式

QQ:[email protected]

猜你喜欢

转载自blog.csdn.net/rjgcszlc/article/details/80977284