onTouchEvent返回多个指针的相同坐标 [英] onTouchEvent returning same coordinates for multiple pointers

查看:142
本文介绍了onTouchEvent返回多个指针的相同坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发我的第一个多点触控Android应用程序,我在使用onTouchEvent()时遇到了一些困难。我已经使用了在线教程中的一些代码,它似乎为我在屏幕上的每次触摸提供了正确的指针ID信息,但ACTION_POINTER_DOWN事件的事件坐标似乎总是与初始触摸的坐标相同。我的代码如下:

I'm currently working on my first multi-touch android application and I'm having some difficulty with the onTouchEvent(). I've used some code from an online tutorial, which seems to give me the right pointer ID information for each touch on the screen, but the event coordinates for ACTION_POINTER_DOWN events always seem to be the same as the coordinates for the intial touch. My code is below:

private int getIndex(MotionEvent event) {
          int idx = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
          return idx;
}

@Override

public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        switch(action) {
                case MotionEvent.ACTION_DOWN : {
                        int id = event.getPointerId(0);
                        Log.d("CV", "Point number " +id+ " is down at X value " +event.getX());
                        callbackListener.onTouchDown(event, id);
                        break;
                }
                case MotionEvent.ACTION_MOVE : {
                        int touchCounter = event.getPointerCount();
                        for (int t = 0; t < touchCounter; t++) {
                                int id = event.getPointerId(t);
                                callbackListener.onMove(event, id);
                        }
                        break;
                }
                case MotionEvent.ACTION_POINTER_DOWN : {
                        int id = event.getPointerId(getIndex(event));
                        Log.d("CV", "Point number " +id+ " is down at X value " +event.getX());
                        callbackListener.onTouchDown(event, id);
                        break;
                }
                case MotionEvent.ACTION_POINTER_UP : {
                        int id = event.getPointerId(getIndex(event));
                        //Log.d("CV", "Other point up ["+id+"]");
                        callbackListener.onTouchUp(event, id);
                        break;
                }
                case MotionEvent.ACTION_UP : {
                        int id = event.getPointerId(0);
                        //Log.d("CV", "Pointer up ["+id+"]");
                        callbackListener.onTouchUp(event, id);
                        break;
                }
        }
        return true;
}

在日志中,我的指针X位置显示,你可以看到当我在屏幕上添加一个额外的触摸(指针编号1)时,X坐标与第一次触摸(指针编号0)相同...

In the Log, where my pointer X positions are shown, you can see that when I add an additional touch to the screen (pointer number 1), the X coordinate is the same as the first touch (pointer number 0)...

11-25 12:34:02.911: D/CV(25231): Point number 0 is down at X value 260.60608
11-25 12:34:05.281: D/CV(25231): Point number 0 is down at X value 477.57578
11-25 12:34:06.261: D/CV(25231): Point number 0 is down at X value 581.8182
11-25 12:34:11.891: D/CV(25231): Point number 0 is down at X value 267.87878
11-25 12:34:13.321: D/CV(25231): Point number 1 is down at X value 267.87878

这让我感到很沮丧,因为我几乎从网上教程中复制并粘贴了整个内容,我可以'看看为什么它不能正常工作!此外,你可以想象,ACTION_MOVE无法正常工作......

This is frustrating me as I've pretty much copied and pasted the entire thing from online tutorials and I can't see why its not working properly! Also, as you can imagine, the ACTION_MOVE isn't working properly either...

非常感谢任何帮助,谢谢

Any help is much appreciated, thanks

推荐答案

这是因为 event.getX()首先返回的值指针索引。

This is because event.getX() returns value for the first pointer index.

您应该使用 getX(int pointerIndex) 如下:

You should be using, getX(int pointerIndex) as follows:

Log.d("CV", "Point number " +id+ " is down at X value " +event.getX(getIndex(event)));

这篇关于onTouchEvent返回多个指针的相同坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆