android – 如何在ACTION_MOVE之后让我的视图停止听取触摸事件?

android – 如何在ACTION_MOVE之后让我的视图停止听取触摸事件?,第1张

概述我有一个按钮,我按下按钮动画.我希望它在超出某个阈值之后快速恢复到“正常”状态. 我在ACTION_DOWN上创建了一个视图边界的矩形,并检查它是否在ACTION_MOVE的触摸区域之外.我成功地检测到了“越界”的触摸,但我无法让视图停止听触摸.这就像它忽略了我的animateToNormal()方法. 我已经尝试将布尔返回值更改为true而不是false,这没有帮助.我也尝试在ACTION_MO 我有一个按钮,我按下按钮动画.我希望它在超出某个阈值之后快速恢复到“正常”状态.

@H_403_8@

我在ACTION_DOWN上创建了一个视图边界的矩形,并检查它是否在ACTION_MOVE的触摸区域之外.我成功地检测到了“越界”的触摸,但我无法让视图停止听触摸.这就像它忽略了我的animatetonormal()方法.@H_403_8@

我已经尝试将布尔返回值更改为true而不是false,这没有帮助.我也尝试在ACTION_MOVE情况下删除触摸侦听器(设置为null),但我需要重新连接以继续听触摸.我想我可以在添加它之前添加一个任意延迟,但这似乎是一个可怕的黑客.@H_403_8@

我正在4.2设备(LG G2)上进行测试.@H_403_8@

@H_403_8@

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;            }        }
解决方法 为什么你只是不继续听,而是设置一个声明来忽略运动事件?

@H_403_8@

像这样的东西:@H_403_8@

@H_403_8@

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.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 – 如何在ACTION_MOVE之后让我的视图停止听取触摸事件?全部内容,希望文章能够帮你解决android – 如何在ACTION_MOVE之后让我的视图停止听取触摸事件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1122191.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存