android 如何在从另一个视图进入时捕获视图上的触摸事件

wqlqzqxt  于 2023-01-15  发布在  Android
关注(0)|答案(2)|浏览(155)

我想捕获用户将手指从一个视图滑到另一个视图时的事件。
我能够通过以下代码捕获手指的运动

view1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(MainActivity.this, "touch listener", Toast.LENGTH_SHORT).show();
            Log.e("touch event", "triggered");
            return true;
        }
    });

但只有当用户从同一视图(即视图1)开始手指移动时,它才起作用。我想捕捉视图1中的手指移动,即使用户从视图2进入。至少用户进入视图1时的第一个事件就足够了。但当我在视图1中移动来自视图2的手指时,触摸事件根本不触发。

1l5u6lss

1l5u6lss1#

每当当前所选触控触点的位置、压力或大小发生变化时,都会通过ACTION MOVE事件调用新的onTouchEvent()函数。所有这些事件都保存在onTouchEvent的MotionEvent参数中,如检测常用手势()中所述。
检测触摸事件通常更多地依赖于移动,而不是简单的接触,因为基于手指的触摸并不总是最准确的参与度方法。Android包含“触摸倾斜”的概念,以帮助应用区分基于移动的手势(如滑动)和非移动手势术语“触摸倾斜”描述了用户的触摸在被识别为基于移动的手势之前可能偏离的像素数。

public class MainActivity extends Activity {
    private static final String DEBUG_TAG = "Velocity";
        ...
    private VelocityTracker mVelocityTracker = null;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the
                    // velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                // When you want to determine the velocity, call
                // computeCurrentVelocity(). Then call getXVelocity()
                // and getYVelocity() to retrieve the velocity for each pointer ID.
                mVelocityTracker.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                Log.d("", "X velocity: " + mVelocityTracker.getXVelocity(pointerId));
                Log.d("", "Y velocity: " + mVelocityTracker.getYVelocity(pointerId));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }
}
0md85ypi

0md85ypi2#

final View view1 = findViewById(R.id.view1);
    final Rect rectOfView1 = new Rect();
    view1.post(() -> view1.getGlobalVisibleRect(rectOfView1)); // populating the rectOfView1 after the view1 is inflated

    final View view2 = findViewById(R.id.view2);
    view2.setOnTouchListener((v, event) -> {
        if (rectOfView1.contains((int) event.getRawX(), (int) event.getRawY())) {
            Log.d("view1 event", "triggered");
            return true;
        }
        Log.d("view2 event", "triggered");
        return true;
    });

相关问题