跟踪 ViewGroup 中的 MotionEvent

发布于 2024-11-25 01:51:24 字数 658 浏览 3 评论 0原文

我有一个扩展 ViewGroup 的类,并希望从中获取每个 MotionEvent 。到目前为止,我有这样的:

class TestViewGroup extends ViewGroup {
     public TestViewGroup(Context context) {
         super(context);
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {
         Log.d("TestViewGroup", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
         return true;
     }
 }

每次我将手指放在屏幕上时, onTouchEvent(MotionEvent event) 方法都能够捕获 MotionEvent 。但是,如果我在手指仍然向下的情况下在屏幕上移动手指,它不会继续追踪我手指的坐标。我知道在扩展 View 的类中,可以在手指在 View 中移动时继续跟踪手指。我只是想知道如何将相同的想法应用于 ViewGroup 。

I have a class extends ViewGroup and want to get every MotionEvent from it. So far I have this:

class TestViewGroup extends ViewGroup {
     public TestViewGroup(Context context) {
         super(context);
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {
         Log.d("TestViewGroup", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
         return true;
     }
 }

The onTouchEvent(MotionEvent event) method is able to capture a MotionEvent every time I place my finger on the screen. But if I move my finger around the screen while my finger is still down, it won't continue to trace the coordinates of my finger. I know that in a class that extends a View, it is possible to keep tracing the finger as it moves through the View. I'm just wondering how it is possible to apply the same idea to a ViewGroup.

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

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

发布评论

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

评论(2

梦里人 2024-12-02 01:51:24

我实现了一个非常简单的 ViewGroup 类并复制了您的代码,onTouchEvent 工作正常

我做的唯一不同的是实现所有构造函数,尽管一般来说我也会为 onTochEvent 类调用 super ,这似乎没有什么区别。

所以我想知道您是否遗漏了任何可能会产生影响的代码/xml?

捕获模拟器和设备上的所有触摸事件,与您的几乎一样,但构造函数除外。

public class customViewGroup extends ViewGroup {

    public customViewGroup(Context context) {
        super(context,null,0);
   }

    public customViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs,0);

    }

    public customViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
        return true;//super.onTouchEvent(event);
    }

}

I implemented a very simple ViewGroup class and copied your code, the onTouchEvent worked fine

The only thing I did differently was implement all of the constructors, though in general I would also call the super for the onTochEvent class that did not seem to make a difference.

So I am wondering if there is any code/xml you are leaving out that might make a difference?

Captures all touch events in emulator and on device, pretty much just like yours with the exception of the constructors.

public class customViewGroup extends ViewGroup {

    public customViewGroup(Context context) {
        super(context,null,0);
   }

    public customViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs,0);

    }

    public customViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
        return true;//super.onTouchEvent(event);
    }

}
伤感在游骋 2024-12-02 01:51:24

您应该添加 viewGroup.setClickable(true); 以确保视图可以接收更多触摸事件。

you should add viewGroup.setClickable(true); to make sure the view can receive more touchevent.

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