拖动(只要view大于屏幕,就会自动移动屏幕getScrollX)拖动使用scroller computerScroll()回调函数

来源:互联网 发布:linux中怎么查看主机ip 编辑:程序博客网 时间:2024/05/15 03:36

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

[java] view plaincopy
  1. // The ‘active pointer’ is the one currently moving our object.  
  2. private int mActivePointerId = INVALID_POINTER_ID;  
  3.   
  4. @Override  
  5. public boolean onTouchEvent(MotionEvent ev) {  
  6.     // Let the ScaleGestureDetector inspect all events.  
  7.     mScaleDetector.onTouchEvent(ev);  
  8.                
  9.     final int action = MotionEventCompat.getActionMasked(ev);   
  10.           
  11.     switch (action) {   
  12.     case MotionEvent.ACTION_DOWN: {  
  13.         final int pointerIndex = MotionEventCompat.getActionIndex(ev);   
  14.         final float x = MotionEventCompat.getX(ev, pointerIndex);   
  15.         final float y = MotionEventCompat.getY(ev, pointerIndex);   
  16.               
  17.         // Remember where we started (for dragging)  
  18.         mLastTouchX = x;  
  19.         mLastTouchY = y;  
  20.         // Save the ID of this pointer (for dragging)  
  21.         mActivePointerId = MotionEventCompat.getPointerId(ev, 0);  
  22.         break;  
  23.     }  
  24.               
  25.     case MotionEvent.ACTION_MOVE: {  
  26.         // Find the index of the active pointer and fetch its position  
  27.         final int pointerIndex =   
  28.                 MotionEventCompat.findPointerIndex(ev, mActivePointerId);    
  29.               
  30.         final float x = MotionEventCompat.getX(ev, pointerIndex);  
  31.         final float y = MotionEventCompat.getY(ev, pointerIndex);  
  32.               
  33.         // Only move if the ScaleGestureDetector isn't processing a gesture.  
  34.         if (!mScaleDetector.isInProgress()) {  
  35.             // Calculate the distance moved  
  36.             final float dx = x - mLastTouchX;  
  37.             final float dy = y - mLastTouchY;  
  38.   
  39.             mPosX += dx;  
  40.             mPosY += dy;  
  41.   
  42.             invalidate();  
  43.         }  
  44.         // Remember this touch position for the next move event  
  45.         mLastTouchX = x;  
  46.         mLastTouchY = y;  
  47.   
  48.         break;  
  49.     }  
  50.               
  51.     case MotionEvent.ACTION_UP: {  
  52.         mActivePointerId = INVALID_POINTER_ID;  
  53.         break;  
  54.     }  
  55.               
  56.     case MotionEvent.ACTION_CANCEL: {  
  57.         mActivePointerId = INVALID_POINTER_ID;  
  58.         break;  
  59.     }  
  60.           
  61.     case MotionEvent.ACTION_POINTER_UP: {  
  62.               
  63.         final int pointerIndex = MotionEventCompat.getActionIndex(ev);   
  64.         final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);   
  65.   
  66.         if (pointerId == mActivePointerId) {  
  67.             // This was our active pointer going up. Choose a new  
  68.             // active pointer and adjust accordingly.  
  69.             final int newPointerIndex = pointerIndex == 0 ? 1 : 0;  
  70.             mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);   
  71.             mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);   
  72.             mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);  
  73.         }  
  74.         break;  
  75.     }  
  76.     }         
  77.     return true;  
  78. }  
0 0
原创粉丝点击