首页 > 移动平台 > 详细

Android “swipe” vs “fling”

时间:2014-05-26 06:23:18      阅读:395      评论:0      收藏:0      [点我收藏+]

  onFling will get executed when a user makes a "fling" motion, and said motion has a velocity with it to determine the type of fling it was. However,

if a user simply touches the device and moves slowly across the screen, that would not be considered a fling, but a swipe.

 

  It comes down to what type of motion you expect the users to perform. The ideal case would be to implement the onFling function to capture that

motion, and also implement onDrag and onDragFinished to capture the more subtle motions that should still be considered a swipe.

 

bubuko.com,布布扣
public class MyActivity extends Activity {
    private void onCreate() {
        final GestureDetector gdt = new GestureDetector(new GestureListener());
        final ImageView imageView  = (ImageView) findViewById(R.id.image_view);
        imageView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                gdt.onTouchEvent(event);
                return true;
            }
        });
    }               

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private class GestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Right to left
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Left to right
            }

            if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Bottom to top
            }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                return false; // Top to bottom
            }
            return false;
        }
    }
}
bubuko.com,布布扣

 

Android “swipe” vs “fling”,布布扣,bubuko.com

Android “swipe” vs “fling”

原文:http://www.cnblogs.com/yuyutianxia/p/3748024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!