简体   繁体   English

ACTION_CANCEL 未触发

[英]ACTION_CANCEL not fired

I implemented a customView to add a little animation when a view is touched and relieved.我实现了一个 customView 以在触摸和释放视图时添加一点 animation。 When the user click on this view, i launch a small animation of 80ms which shrink the view.当用户点击这个视图时,我会启动一个 80 毫秒的小 animation 来缩小视图。

When the finger is up, the same animation is played (reverse effect).当手指向上时,播放相同的 animation(反向效果)。 The visual effect is great, and it works properly as long as the user don't leave the view by moving his finger.视觉效果很好,只要用户不移动手指就可以正常工作。

Therefore, i just can't get any call to ACTION_CANCEL when the finger leave the view area, which is needed in my case to play the animation back.因此,当手指离开视图区域时,我无法收到任何对 ACTION_CANCEL 的调用,在我的情况下,这是播放 animation 所必需的。

ACTION_UP, ACTION_MOVE and ACTION_DOWN are properly called, but never ACTION_CANCEL正确调用 ACTION_UP、ACTION_MOVE 和 ACTION_DOWN,但从不调用 ACTION_CANCEL

Here's my code:这是我的代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_CANCEL: {
            Animation anim = new ScaleAnimation(0.9f, 1f, 0.9f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setDuration(Utils.ANIM_CLICK_LENGHT);
            anim.setFillAfter(true);
            anim.setFillBefore(true);
            startAnimation(anim);
            break;
        }
        case MotionEvent.ACTION_DOWN: {
            Animation anim = new ScaleAnimation(1f, 0.9f, 1f, 0.9f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setDuration(Utils.ANIM_CLICK_LENGHT);
            anim.setFillAfter(true);
            anim.setFillBefore(true);
            startAnimation(anim);
            break;
        }
        case MotionEvent.ACTION_UP: {
            Animation anim = new ScaleAnimation(0.9f, 1f, 0.9f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setDuration(Utils.ANIM_CLICK_LENGHT);
            anim.setFillAfter(true);
            anim.setFillBefore(true);
            startAnimation(anim);
            break;
        }
    }
    return true;
}

Check this answer: https://stackoverflow.com/a/20160587/1576319 检查此答案: https : //stackoverflow.com/a/20160587/1576319

It explains in detail why ACTION_CANCEL is not fired up in API>16 and provides a quick workaround. 它详细说明了为什么未在API> 16中启动ACTION_CANCEL并提供了一种快速的解决方法。

Kotlin Answer Solved it by using MotionEvent.ACTION_UP and MotionEvent.ACTION_CANCEL in a when case:科特林回答的情况下,当使用MotionEvent.ACTION_UP和MotionEvent.ACTION_CANCEL解决它:

when (event.action) {
    MotionEvent.ACTION_DOWN -> startBrushEvent()
    MotionEvent.ACTION_MOVE -> useBrush(image as ImageView, event)
    MotionEvent.ACTION_UP,
    MotionEvent.ACTION_CANCEL ->
        stopBrushEvent()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM