简体   繁体   English

Android:在 ACTION_CANCEL 之后不触发 MotionEvent ACTION_UP

[英]Android: MotionEvent ACTION_UP doesn't trigger after ACTION_CANCEL

I'm making video stories like instagram.我正在制作像 instagram 这样的视频故事。 So I met a problem with gestures.所以我遇到了手势问题。

The problem is that ACTION_CANCEL handled when I'm doing these moves and ACTION_UP doesn't call if I raise my finger问题是当我做这些动作时 ACTION_CANCEL 被处理,如果我举起手指 ACTION_UP 不会调用

  1. I'm in 1st page of ViewPager and I swipe left->right fastly (my finger still on screen)我在 ViewPager 的第一页,我快速向左->向右滑动(我的手指仍在屏幕上)
  2. I'm in the middle of ViewPager and I swipe left->right or right->left, but not finishing the swipe and I'm still in the current page and my finger on screen我在 ViewPager 的中间,我向左->向右或向右->向左滑动,但没有完成滑动,我仍然在当前页面中,我的手指在屏幕上
  3. I'm moving chaosly on screen (my finger still on screen)我在屏幕上乱动(我的手指还在屏幕上)

So if I raise my finger after ACTION_CANCEL called, my video stay in "PAUSE" state因此,如果我在调用 ACTION_CANCEL 后抬起手指,我的视频将保持“暂停”状态

Finally, question is: How I can handle Action_Up event after Action_Cancel?最后,问题是:如何在 Action_Cancel 之后处理 Action_Up 事件?

override fun onTouch(v: View?, event: MotionEvent?): Boolean {
    if (gestureDetector?.onTouchEvent(event) == true) return true

    when (event?.actionMasked) {
        MotionEvent.ACTION_DOWN -> {
            viewModel.videoPause()
        }
        MotionEvent.ACTION_UP -> {
            viewModel.videoResume()
        }
        MotionEvent.ACTION_CANCEL -> {
            // Handles when doing these moves and ACTION_UP doesn't call if I raise my finger
            // 1. I'm in 1st page of ViewPager and I swipe left->right fastly (my finger still on screen)
            // 2. I'm in the middle of ViewPager and I swipe left->right or right->left, but not finishing the swipe
            // and I'm still in the current page and my finger on screen
            // 3. I'm moving chaosly on screen (my finger still on screen)


            // So if I raise my finger after ACTION_CANCEL called, my video stay in "PAUSE" state
        }
        else -> { }
    }

    return true
}

用户故事第一页 用户故事滑动

You don't get an ACTION_UP after an ACTION_CANCEL :ACTION_CANCEL之后你没有得到ACTION_UP

ACTION_CANCEL

public static final int ACTION_CANCEL

Constant for getActionMasked() : The current gesture has been aborted. getActionMasked()的常量:当前手势已中止。 You will not receive any more points in it.您将不会再获得任何积分。 You should treat this as an up event, but not perform any action that you normally would.您应该将此视为向上事件,但不要执行您通常会执行的任何操作。

Actually there is a way to do this.实际上有一种方法可以做到这一点。

@Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        performClick();
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                float y = event.getY();
                Log.d("coordinates: ","x : "+String.valueOf(x)+"y : " + String.valueOf(y));
                break;
            case MotionEvent.ACTION_UP:
                Log.d("action","up")
        }
        return true;
    }

Add this to your parent View.将此添加到您的父视图。 This will show you the location of your finger whenever it moves.这将在手指移动时向您显示手指的位置。 And (I don't know how) but when you hold your finger, it also triggers the action up.而且(我不知道如何)但是当您握住手指时,它也会触发动作。 So whenever the action up is triggered, you'll know that the user held his finger, or whenever the x or y coordinates are outside of your parent view, you can treat it as if the user held his finger.因此,只要触发了向上操作,您就会知道用户握住了他的手指,或者每当 x 或 y 坐标超出您的父视图时,您就可以将其视为用户握住了他的手指。 (I got this idea from a canvas question [https://stackoverflow.com/questions/21846584/android-canvas-draw-by-finger]) (我从一个画布问题 [https://stackoverflow.com/questions/21846584/android-canvas-draw-by-finger] 中得到了这个想法)

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

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