您的位置:首页 > 移动开发 > Android开发

Android触控基础:MotionEvent

2016-07-25 11:26 381 查看
原文章地址:http://blog.csdn.net/bigconvience/article/details/26611003

常见的动作常量:

    public static final int ACTION_DOWN             = 0;单点触摸动作

    public static final int ACTION_UP               = 1;单点触摸离开动作

    public static final int ACTION_MOVE             = 2;触摸点移动动作

    public static final int ACTION_CANCEL           = 3;触摸动作取消

    public static final int ACTION_OUTSIDE          = 4;触摸动作超出边界

    public static final int ACTION_POINTER_DOWN     = 5;多点触摸动作

    public static final int ACTION_POINTER_UP       = 6;多点离开动作

一些非touch事件:
    public static final int ACTION_HOVER_MOVE       = 7;
    public static final int ACTION_SCROLL           = 8;
    public static final int ACTION_HOVER_ENTER      = 9;
    public static final int ACTION_HOVER_EXIT       = 10;

下面的代码段能使用户在屏幕上拖动一个对象。它记录了初始点的位置,计算点移动的距离,并将对象移动到新的位置。它正确的处理了这种情况:当第一个手指把控件拖到一个位置,然后按下第二个手指,且第二个手指与同一个控件上。当用户抬起第一个手指时,控件不会跑到第二个手指的位置同时第二个手指可以继续拖动控件。

下面的代码段能使用户在屏幕上拖动一个对象。它记录了初始点的位置,计算点移动的距离,并将对象移动到新的位置。它正确的处理了这种情况:当第一个手指把控件拖到一个位置,然后按下第二个手指,且第二个手指与同一个控件上。当用户抬起第一个手指时,控件不会跑到第二个手指的位置同时第二个手指可以继续拖动控件。

[java] view
plain copy

// The ‘active pointer’ is the one currently moving our object.  

private int mActivePointerId = INVALID_POINTER_ID;  

  

@Override  

public boolean onTouchEvent(MotionEvent ev) {  

    // Let the ScaleGestureDetector inspect all events.  

    mScaleDetector.onTouchEvent(ev);  

               

    final int action = MotionEventCompat.getActionMasked(ev);   

          

    switch (action) {   

    case MotionEvent.ACTION_DOWN: {  

        final int pointerIndex = MotionEventCompat.getActionIndex(ev);   

        final float x = MotionEventCompat.getX(ev, pointerIndex);   

        final float y = MotionEventCompat.getY(ev, pointerIndex);   

              

        // Remember where we started (for dragging)  

        mLastTouchX = x;  

        mLastTouchY = y;  

        // Save the ID of this pointer (for dragging)  

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);  

        break;  

    }  

              

    case MotionEvent.ACTION_MOVE: {  

        // Find the index of the active pointer and fetch its position  

        final int pointerIndex =   

                MotionEventCompat.findPointerIndex(ev, mActivePointerId);    

              

        final float x = MotionEventCompat.getX(ev, pointerIndex);  

        final float y = MotionEventCompat.getY(ev, pointerIndex);  

              

        // Only move if the ScaleGestureDetector isn't processing a gesture.  

        if (!mScaleDetector.isInProgress()) {  

            // Calculate the distance moved  

            final float dx = x - mLastTouchX;  

            final float dy = y - mLastTouchY;  

  

            mPosX += dx;  

            mPosY += dy;  

  

            invalidate();  

        }  

        // Remember this touch position for the next move event  

        mLastTouchX = x;  

        mLastTouchY = y;  

  

        break;  

    }  

              

    case MotionEvent.ACTION_UP: {  

        mActivePointerId = INVALID_POINTER_ID;  

        break;  

    }  

              

    case MotionEvent.ACTION_CANCEL: {  

        mActivePointerId = INVALID_POINTER_ID;  

        break;  

    }  

          

    case MotionEvent.ACTION_POINTER_UP: {  

              

        final int pointerIndex = MotionEventCompat.getActionIndex(ev);   

        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);   

  

        if (pointerId == mActivePointerId) {  

            // This was our active pointer going up. Choose a new  

            // active pointer and adjust accordingly.  

            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;  

            mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);   

            mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);   

            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);  

        }  

        break;  

    }  

    }         

    return true;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: