zoukankan      html  css  js  c++  java
  • Android “swipe” vs “fling”

      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.

    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;
            }
        }
    }
  • 相关阅读:
    js函数柯理化
    Promise异步编程解决方案
    set和map结构,class类
    原创:用node.js搭建本地服务模拟接口访问实现数据模拟
    原创:微信小程序如何使用自定义组件
    原创:微信小程序开发要点总结
    Nodejs CMS——基于 NestJS/NuxtJS 的完整开源项目
    浅谈js对象之数据属性、访问器属性、Object.defineProperty方法
    Promise初步详解(resolve,reject,catch)
    原生js面向对象实现简单轮播
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3748024.html
Copyright © 2011-2022 走看看