简体   繁体   English

Android 多点触控 View.OnTouchListener

[英]Android multitouch View.OnTouchListener

Im trying to play around with some multitouch in Android.我试图在 Android 中尝试一些多点触控。

I have two RelativeLayout on screen where I would like to get touch events.我在屏幕上有两个 RelativeLayout,我想在其中获取触摸事件。

fooLayout = (RelativeLayout)findViewById(R.id.foo);
fooLayout.setOnTouchListener(onTouchListener);

barLayout = (RelativeLayout)findViewById(R.id.bar);
barLayout.setOnTouchListener(onTouchListener);

...

View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    ...

I tried having a OnTouchListener for each layout but that didn't seem to work at all.我尝试为每个布局设置一个 OnTouchListener ,但这似乎根本不起作用。

I figured since the onTouch method gets an View I would be able to make out with view the current touch event came from and save away the pointer id.我想既然 onTouch 方法获得了一个视图,我就可以通过视图确定当前的触摸事件来自并保存指针 id。

int pointerId = event.getPointerId(event.getActionIndex());
int action = event.getActionMasked();
switch (action) {
     case MotionEvent.ACTION_DOWN:
     case MotionEvent.ACTION_POINTER_DOWN:
          switch (v.getId()) {
               case R.id.foo:
                    fooPointerId = pointerId;
                    break;
               case R.id.bar:
                    barPointerId = pointerId;
                    break;
}

This does not work as I thought it would.这并不像我想象的那样工作。 When my first finger touches in one of the layouts I will get the correct id from that layout.当我的第一根手指接触其中一个布局时,我将从该布局中获得正确的 id。 But when I put down a second finger in the other layout it will get the id of the layout where I put down my first finger.但是当我在另一个布局中放下第二根手指时,它会得到我放下第一根手指的布局的 id。

Am I going about this the wrong way?我会以错误的方式解决这个问题吗?

Is this possible to do or do I have to put the OnTouchListener on the top view and figure out which layout Im touching by the x/y values of the event?这是可能的,还是我必须将 OnTouchListener 放在顶视图上,并通过事件的 x/y 值找出我触摸的布局?

I think you should extend relative layout and implement onTouchEvent(MotionEvent ev) over there to figure out which pointers are touching which child views(Put foo and bar inside your main relative layout/view group that implements onTouchEvent).我认为您应该扩展相对布局并在那里实现 onTouchEvent(MotionEvent ev) 以找出哪些指针正在接触哪些子视图(将 foo 和 bar 放在实现 onTouchEvent 的主要相对布局/视图组中)。

IMO onTouch(View v, MotionEvent ev) only lets you register one pointer at a time so it may not be possible using this approach. IMO onTouch(View v, MotionEvent ev) 只允许您一次注册一个指针,因此使用这种方法可能无法实现。 This link might help out链接可能会有所帮助

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

相关问题 Android数据绑定view.onTouchListener - Android data binding view.onTouchListener 如何使用View.OnTouchListener代替onClick - How to use View.OnTouchListener instead of onClick View.OnTouchListener()不适用于父布局 - View.OnTouchListener() does not work on parent layout View.OnTouchListener和屏幕旋转协调 - View.OnTouchListener and screen rotation coordination 在自定义控件上实现View.OnTouchListener - Implement View.OnTouchListener on custom control Android Studio:如何在java中获取自定义按钮(按下/未按下)的状态,就像我可以使用View.onTouchListener获取常规按钮一样 - Android Studio: how do I get the state of a custom button (Pressed/Not Pressed) in java like I can with View.onTouchListener for regular buttons View.OnTouchListener是否在UI线程之外的其他线程上运行? - Does View.OnTouchListener run on other thread than the UI thread? Android:在MultiTouch中未调用OnTouchListener.OnTouch - Android: OnTouchListener.OnTouch not called in multiTouch 无法从视图类型静态引用非静态方法setOnTouchListener(View.OnTouchListener) - Cannot make a static reference to the non-static method setOnTouchListener(View.OnTouchListener) from the type View Jetpack Compose 中的“View.onTouchListener”等价物是什么? 我需要触摸坐标 - What is the "View.onTouchListener" equivalent in Jetpack Compose? I need the touch coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM