记录每天学习的新知识:判断三指滑动

多指滑动

  • 判断手指数量
    • 方法一,通过 event.getPointerCount() 判断手指数量
    • 方法二,通过 event.getAction() 判断手指数量
  • 判断三指滑动
    • 场景1,使用根布局判断的场景
    • 场景2,使用RecyclerView判断的场景

判断手指数量

多指的判断源于方法 onTouch,通过其内部参数判断手指数量。如下两个方法,有的屏幕仅支持一种,需要尝试下。

方法一,通过 event.getPointerCount() 判断手指数量

        getWindow().getDecorView().setOnTouchListener((v, event) -> {pointerCount = event.getPointerCount();Log.i(TAG, "pointerCount: " + pointerCount);return false;});

输出:

2022-02-23 10:35:53.161 20276-20276/com.yoshin.tspsdk I/Gesture2Activity: pointerCount: 1
2022-02-23 10:35:53.166 20276-20276/com.yoshin.tspsdk I/Gesture2Activity: pointerCount: 2
2022-02-23 10:35:53.169 20276-20276/com.yoshin.tspsdk I/Gesture2Activity: pointerCount: 3
2022-02-23 10:35:53.179 20276-20276/com.yoshin.tspsdk I/Gesture2Activity: pointerCount: 3

3,代表有三根手指

方法二,通过 event.getAction() 判断手指数量

        getWindow().getDecorView().setOnTouchListener((v, event) -> {int action = event.getAction();switch (action & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:Log.i(TAG, "getDecorView.onTouch ACTION_DOWN");break;case MotionEvent.ACTION_MOVE:Log.i(TAG, "getDecorView.onTouch ACTION_MOVE");break;case MotionEvent.ACTION_POINTER_DOWN:gestureDetectorModel++;Log.i(TAG, "getDecorView.onTouch ACTION_POINTER_DOWN");break;case MotionEvent.ACTION_POINTER_UP:gestureDetectorModel--;Log.i(TAG, "getDecorView.onTouch ACTION_POINTER_UP");break;case MotionEvent.ACTION_UP:Log.i(TAG, "getDecorView.onTouch ACTION_UP");break;case MotionEvent.ACTION_CANCEL:Log.i(TAG, "getDecorView.onTouch ACTION_CANCEL");break;}Log.i(TAG, "gestureDetectorModel: " + gestureDetectorModel);return false;});

输出:

2022-02-23 10:39:17.642 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: getDecorView.onTouch ACTION_DOWN
2022-02-23 10:39:17.643 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: gestureDetectorModel: 0
2022-02-23 10:39:17.649 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: getDecorView.onTouch ACTION_POINTER_DOWN
2022-02-23 10:39:17.649 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: gestureDetectorModel: 1
2022-02-23 10:39:17.652 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: getDecorView.onTouch ACTION_POINTER_DOWN
2022-02-23 10:39:17.652 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: gestureDetectorModel: 2
2022-02-23 10:39:17.654 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: getDecorView.onTouch ACTION_CANCEL
2022-02-23 10:39:17.655 20692-20692/com.yoshin.tspsdk I/Gesture2Activity: gestureDetectorModel: 2

gestureDetectorModel 的数量是2的时候,代表有三根手指接触屏幕

MotionEvent.ACTION_POINTER_DOWN 触发一次,代表是两根手指触屏;
MotionEvent.ACTION_POINTER_DOWN 触发两次,代表是三根手指触屏。

MotionEvent.ACTION_POINTER_UP 触发一次,代表有一根手指离开屏幕,且MotionEvent.ACTION_POINTER_UP 有的屏幕会混乱触发,可以通过测试判断是否用。

判断三指滑动

场景1,使用根布局判断的场景

    private int pointerCount = 0;private boolean finish3Finger;//用于判断是否消耗了事件@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_gesture_2);Gesture2listener gesturelistener = new Gesture2listener ();final GestureDetector detector = new GestureDetector(this, gesturelistener);getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {int action = event.getAction();switch (action & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_UP:Log.i(TAG, "getDecorView.onTouch ACTION_UP");finish3Finger = false;break;}pointerCount = event.getPointerCount();return detector.onTouchEvent(event);}});}private class Gesture2listener extends GestureDetector.SimpleOnGestureListener {@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {Log.i(TAG, "onScroll pointerCount ==" + pointerCount);if (pointerCount == 3) {if (finish3Finger) return true;// 主屏右滑,切到副屏;副屏左滑,切到主屏if (e2.getX() - e1.getX() > 100) {//右滑 正Log.i(TAG, "onScroll 右滑 正");finish3Finger = true;}if (e2.getX() - e1.getX() < -100) {// 左滑 负Log.i(TAG, "onScroll 左滑 负");finish3Finger = true;}}return false;}}

场景2,使用RecyclerView判断的场景

有列表的情况也需要判断三指滑动,重写一下recyclerview就好了

public class TouchPenetrateRecyclerView extends RecyclerView {private float startX;private float endX;private int pointerCount = 0;private boolean trigger = false;private static final String TAG = "TouchPenetrateRecyclerView";public TouchPenetrateRecyclerView(@NonNull Context context) {super(context);}public TouchPenetrateRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public TouchPenetrateRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean onTouchEvent(MotionEvent e) {AppLogUtils.input(TAG, "onTouchEvent =" + e.getPointerCount());if (e.getPointerCount() == 3) {if (pointerCount != 3) {startX = e.getX();pointerCount = 3;} else {endX = e.getX();}} else {pointerCount = 0;startX = 0;endX = 0;trigger = false;}if (startX != 0 && endX != 0) {//右滑 正 ;左滑 负if (endX - startX > 100) {//回调事件if (!trigger)if (onThreeFingerScrollCallback != null)onThreeFingerScrollCallback.onRightSlip();trigger = true;}if (endX - startX < -100) {if (!trigger)if (onThreeFingerScrollCallback != null)onThreeFingerScrollCallback.onLeftSlip();trigger = true;}}if (e.getPointerCount() == 3) return false;return super.onTouchEvent(e);}@Overridepublic boolean onInterceptHoverEvent(MotionEvent event) {return super.onInterceptHoverEvent(event);}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {return super.dispatchTouchEvent(ev);}private OnThreeFingerScrollCallback onThreeFingerScrollCallback;public void setOnThreeFingerScrollCallback(OnThreeFingerScrollCallback onThreeFingerScrollCallback) {this.onThreeFingerScrollCallback = onThreeFingerScrollCallback;}public interface OnThreeFingerScrollCallback {/*** 三指*/void onRightSlip();/*** 左滑*/void onLeftSlip();}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部