Android:MotionEvent Action_up do do and action_cancel do do do

发布于 2025-01-28 11:35:50 字数 1737 浏览 4 评论 0原文

我正在制作Instagram之类的视频故事。所以我遇到了手势的问题。

问题是,当我执行这些动作时,Action_Cancel处理并且Action_up如果抬起手指

  1. ,我会在ViewPager的第一页上打电话,并且我很迅速地向左滑动(我的手指仍然在屏幕上)
  2. 我' m在ViewPager的中间,我向左滑动左或右 - 左或左右,但没有完成滑动,我仍然在当前页面上,我的手指在屏幕上,
  3. 我在屏幕上连混在一起(我的手指仍然在屏幕上),

因此,如果我在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
}

I'm making video stories like 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

  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

Finally, question is: How I can handle Action_Up event after Action_Cancel?

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
}

user stories 1st page
user stories swipe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2025-02-04 11:35:50

> action_cancel

action_cancel

公共静态最终int action_cancel

getActionMasked()的常数:当前的手势已中止。 您将不会收到更多的积分。您应该将其视为一个UP事件,但不要执行您通常会的任何动作。

You don't get an ACTION_UP after an ACTION_CANCEL:

ACTION_CANCEL

public static final int ACTION_CANCEL

Constant for getActionMasked(): The current gesture has been aborted. 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.

乱了心跳 2025-02-04 11:35:50

实际上,有一种方法可以做到这一点。

@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;
    }

将其添加到您的父级视图中。这将在手指移动时向您显示手指的位置。而且(我不知道),但是当您握住手指时,它也会触发动作。因此,每当触发操作时,您就会知道用户握住他的手指,或者每当X或Y坐标不在父级视图之外时,就可以像用户握住手指一样对待它。 (我从一个帆布问题[https://stackoverflow.com/questions/21846584/android-canvas-draw-by finger])

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. (I got this idea from a canvas question [https://stackoverflow.com/questions/21846584/android-canvas-draw-by-finger])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文