[英]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.