android - 如何让我的 View 在 ACTION_MOVE 后停止收听触摸事件?

标签 android android-animation ontouchlistener

我有一个按钮,我在按下按钮时制作动画。我希望它在我拖出某个阈值后迅速恢复到“正常”状态。

我在 ACTION_DOWN 上创建了一个 View 边界矩形,并在 ACTION_MOVE 中检查它是否超出了触摸区域。我成功检测到“越界”触摸,但我无法让 View 停止收听触摸。就像它忽略了我的 animateToNormal() 方法一样。

我已经尝试将 bool 返回值更改为 true 而不是 false,但这没有帮助。我还尝试在 ACTION_MOVE 情况下删除触摸监听器(设置为 null),但我需要重新连接才能继续监听触摸。我认为我可以在将其添加回来之前添加任意延迟,但这似乎是一个糟糕的 hack。

我正在 4.2 设备 (LG G2) 上对此进行测试。

private static class AnimationOnTouchListener implements View.OnTouchListener {
        private Rect rect;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch(motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
                    animatePressed();
                    return false;

                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    // back to normal state
                    animateBackToNormal();
                    return false;

                case MotionEvent.ACTION_MOVE:
                    if(!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())){
                        d(TAG, "out of bounds");
                        animateBackToNormal();
                        // STOP LISTENING TO MY TOUCH EVENTS!

                    } else {
                        d(TAG, "in bounds");
                    }
                    return false;
                default:
                    return true;
            }
        }

最佳答案

为什么不一直监听而是设置一个语句忽略motion事件?

类似的东西:

private static class AnimationOnTouchListener implements View.OnTouchListener {
        private Rect rect;
        private boolean ignore = false;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(ignore && motionEvent.getAction()!=MotionEvent.ACTION_UP)
           return false;
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
                animatePressed();
                return false;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                // back to normal state
                animateBackToNormal();

                // IMPORTANT - touch down won't work if this isn't there.
                ignore = false;
                return false;

            case MotionEvent.ACTION_MOVE:
                if(!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())){
                    d(TAG, "out of bounds");
                    animateBackToNormal();
                    // STOP LISTENING TO MY TOUCH EVENTS!
                    ignore = true;
                } else {
                    d(TAG, "in bounds");
                }
                return false;
            default:
                return true;
        }
    }

关于android - 如何让我的 View 在 ACTION_MOVE 后停止收听触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466041/

相关文章:

android - 为 Android 应用程序托管 MySQL 数据库选项

android - 滑动展开动画

java - Android:动画自定义 View

android - 向右滑动即可结束 Activity ?

Android:如何确定触摸事件在 TextView 中的位置的字符索引?

javascript - 使用 javascript 从 html 中提取元素

android - 如何在 Android 中检查网络内的互联网连接(通过 HOTSPOT 使用其他设备的互联网)

Android 弯曲 View Canvas 以提供 curl 效果

Android 拖动并返回到之前的 Activity ,例如 Facebook 和 Google Photos

android - 触摸 Android 时停止 fakedrag