Android小坑-OnTouchListener()事件监听长按后抬手MotionEvent.ACTION_MOVE不触发问题

场景:
控件使用OnTouchListener()事件监听,正常的流程是,按下瞬间屏幕捕捉到触摸,触发MotionEvent.ACTION_DOWN事件,滑动屏幕会触发MotionEvent.ACTION_MOVE事件,手指离开屏幕会触发MotionEvent.ACTION_UP事件,这是我们所想要的事件触发流程,但是这不是绝对的.
举例:
假如我们的步骤使:按下-长按-松手,出现MotionEvent.ACTION_UP事件不触发的情况,而是触发了MotionEvent.ACTION_CANCEL事件.综上,短暂长按松手时触发流程是:MotionEvent.ACTION_DOWN-(滑动MotionEvent.ACTION_MOVE)-MotionEvent.ACTION_UP,长时间长按的触发流程:MotionEvent.ACTION_DOWN-滑动MotionEvent.ACTION_MOVE-MotionEvent.ACTION_UP),MotionEvent.ACTION_DOWN-不滑动-MotionEvent.ACTION_CANCLE.

解释:
长按的时间多久才会触发MotionEvent.ACTION_CANCLE ?这个博主目前没有一个定论.所以为了避免出错,应该在代码中加入MotionEvent.ACTION_CANCLE的触发操作.


    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        //按钮按下逻辑
        break;
        case MotionEvent.ACTION_UP:
        //按钮弹起逻辑
        break;
        case MotionEvent.ACTION_CANCEL:
        //按钮弹起逻辑
        break;
    }

注:这种情况只会出现在部分机型上,可能在一些机型上不服复现MotionEvent.ACTION_CANCEL的问题,目前已知出现问题的机型有:
安卓9.0,小米5,长按抬起事件是ACTION_CANCEL
安卓8.0,小米6,长按抬起事件是ACTION_CANCEL
安卓6.0,红米4,长按抬起事件是ACTION_CANCEL
安卓6.0,荣耀7,长按抬起事件是ACTION_UP

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.blog.demo11; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import androidx.annotation.Nullable; import com.blog.BaseActivity; import com.blog.R; import static android.view.MotionEvent.INVALID_POINTER_ID; /** * 可拖拽效果类。 */ public class ViewDragActivity extends BaseActivity { private ImageView backgroundImage; private ImageView image1; private ImageView image2; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drag_layout); backgroundImage = findViewById(R.id.background_image); image1 = findViewById(R.id.image1); image2 = findViewById(R.id.image2); // Set touch listeners for each image image1.setOnTouchListener(new MultiTouchListener()); image2.setOnTouchListener(new MultiTouchListener()); } private class MultiTouchListener implements View.OnTouchListener { private float lastTouchX, lastTouchY; private int activePointerId = INVALID_POINTER_ID; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { final int pointerIndex = event.getActionIndex(); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); lastTouchX = x; lastTouchY = y; activePointerId = event.getPointerId(pointerIndex); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = event.findPointerIndex(activePointerId); final float x = event.getX(pointerIndex); final float y = event.getY(pointerIndex); final float dx = x - lastTouchX; final float dy = y - lastTouchY; v.setX(v.getX() + dx); v.setY(v.getY() + dy); lastTouchX = x; lastTouchY = y; break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { activePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerIndex = event.getActionIndex(); final int pointerId = event.getPointerId(pointerIndex); if (pointerId == activePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; lastTouchX = event.getX(newPointerIndex); lastTouchY = event.getY(newPointerIndex); activePointerId = event.getPointerId(newPointerIndex); } break; } } return true; } } }
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值