Android 이벤트 배포를 통해 슬라이딩 Activity 반환

2726 단어
/**
*       
*    Activity       
* @author    
*/
public class SlideBackActivity extends Activity{
     //            
     private static final int YSPEED_MIN = 1000;
     //            
     private static final int XDISTANCE_MIN = 50;
     //              
     private static final int YDISTANCE_MIN = 100;
     //           。
     private float xDown;
     //           。
     private float yDown;
     //           。
     private float xMove;
     //           。
     private float yMove;
     //           。
     private VelocityTracker mVelocityTracker;
     @Override
     public boolean dispatchTouchEvent(MotionEvent event) {
      createVelocityTracker(event);
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        xDown = event.getRawX();
        yDown = event.getRawY();
        break;
      case MotionEvent.ACTION_MOVE:
        xMove = event.getRawX();
        yMove= event.getRawY();
        //     
        int distanceX = (int) (xMove - xDown);
        int distanceY= (int) (yMove - yDown);
        //      
        int ySpeed = getScrollVelocity();
        //  Activity       :
        //1.x      >XDISTANCE_MIN
        //2.y       YDISTANCE_MIN   
        //3.y  (        )<XSPEED_MIN,    ,                   Activity
        if(distanceX > XDISTANCE_MIN &&(distanceY<YDISTANCE_MIN&&distanceY>-YDISTANCE_MIN)&& ySpeed < YSPEED_MIN) {
          finish();
        }
        break;
      case MotionEvent.ACTION_UP:
        recycleVelocityTracker();
        break;
      default:
        break;
      }
      return super.dispatchTouchEvent(event);
    }
    /**
  *   VelocityTracker  ,              VelocityTracker  。
  *
  * @param event
  *
  */
    private void createVelocityTracker(MotionEvent event) {
     if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
     }
     mVelocityTracker.addMovement(event);
    }
    /**
  *   VelocityTracker  。
  */
    private void recycleVelocityTracker() {
      mVelocityTracker.recycle();
      mVelocityTracker = null;
    }
    /**
  *
  * [url=home.php?mod=space&uid=309376]@return[/url]     ,               。
  */
    private int getScrollVelocity() {
      mVelocityTracker.computeCurrentVelocity(1000);
      int velocity = (int) mVelocityTracker.getYVelocity();
      return Math.abs(velocity);
    }
}

원작자 감사:http://www.apkbus.com/forum.php?mod=viewthread&tid=243621&extra=page%3D2 :https : / / github.com / ikew 0 ng / SwipeBackLayout

좋은 웹페이지 즐겨찾기