Android:检测猛击手势

发布于 2024-08-15 08:38:07 字数 2853 浏览 5 评论 0原文

在我的应用程序中,我尝试捕获视图上的 fling 事件(确切地说是 ScrollView,但也尝试使用 LinearLayout)。

通过设置断点,我可以看到 MotionEvents 正确发生到 onFling() 应该触发的位置。

事件流程如下:

  1. 视图的 onTouchEvent
  2. OnGestureListener 的 onDown
  3. OnGestureListener 的 onShowPress
  4. OnGestureListener 的 onLongPress

我是不知道如何继续尝试调试类似的东西,谷歌搜索也没有出现太多。有什么指点吗?

我还附上了应该处理手势的相关视图:

import android.content.Context;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class GestureScrollView extends ScrollView {
    private static int MAJOR_MOVE = 60;
    private GestureDetector gd;

    public GestureScrollView(final Context context) {
        super(context);
        gd = new GestureDetector(context,
                new GestureDetector.OnGestureListener() {

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onShowPress(MotionEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                            float velocityY) {
                        int dx = (int) (e2.getX() - e1.getX());
                        if (Math.abs(dx) > MAJOR_MOVE
                                && Math.abs(velocityX) > Math.abs(velocityY)) {
                            if (velocityX < 0) {
                                Intent intent = new Intent(context, Main.class);
                                context.startActivity(intent);
                            }
                        }
                        return false;
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }
}

In my app I try to capture a fling event on a view (a ScrollView to be exact, but tried with LinearLayout as well).

By setting breakpoints I can see the that the MotionEvents happen correctly up to the point where the onFling() should fire.

The event flow is as follows:

  1. The view's onTouchEvent
  2. The OnGestureListener's onDown
  3. The OnGestureListener's onShowPress
  4. The OnGestureListener's onLongPress

I'm at a loss on how to go on trying to debug something like that and Google search does not turn up much. Any pointers ?

I also attach the relevant view that should handle the gestures:

import android.content.Context;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class GestureScrollView extends ScrollView {
    private static int MAJOR_MOVE = 60;
    private GestureDetector gd;

    public GestureScrollView(final Context context) {
        super(context);
        gd = new GestureDetector(context,
                new GestureDetector.OnGestureListener() {

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onShowPress(MotionEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                            float velocityY) {
                        int dx = (int) (e2.getX() - e1.getX());
                        if (Math.abs(dx) > MAJOR_MOVE
                                && Math.abs(velocityX) > Math.abs(velocityY)) {
                            if (velocityX < 0) {
                                Intent intent = new Intent(context, Main.class);
                                context.startActivity(intent);
                            }
                        }
                        return false;
                    }

                    @Override
                    public boolean onDown(MotionEvent e) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }
}

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

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

发布评论

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

评论(1

农村范ル 2024-08-22 08:38:07

从 Activity 获取 MotionEvent,而不是从 View 获取。当您的手势检测器基于来自 Activity 的 MotionEvent 时,您将获得 onFling 事件。因此,您必须将 onTouchEvent() 从 GestureScrollView 移至 Activity 类。
问候!

Take the MotionEvent from Activity not from your View. When your Gesture detector is based on MotionEvent that comes from Activity you will get the onFling event. So you have to move onTouchEvent() from your GestureScrollView to your Activity class.
Regards!

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