コード例 #1
0
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
       lastX = (int) event.getX();
       lastY = (int) event.getY();
       break;
     case MotionEvent.ACTION_MOVE:
       int offsetX = x - lastX;
       int offsetY = y - lastY;
       ((View) getParent()).scrollBy(-offsetX, -offsetY);
       break;
     case MotionEvent.ACTION_UP:
       // 手指离开时,执行滑动过程
       View viewGroup = ((View) getParent());
       ScrollerHelper.getScroller()
           .startScroll(
               viewGroup.getScrollX(),
               viewGroup.getScrollY(),
               -viewGroup.getScrollX(),
               -viewGroup.getScrollY());
       invalidate();
       break;
   }
   return true;
 }