我想知道如何获得MotionEvent的准确,get(x)和get(y)值?发生的事情是,当我触摸屏幕上的特定区域时,我会告诉您要采取的措施。 问题是,一旦我触摸屏幕并取下手指,它仍然认为我的手指在同一位置(因为那是我触摸的最后一个位置)。因此,当我有多个Down事件(用于多点触控)时,它会抛出一切。有没有办法重置X和Y值,所以当我放开屏幕时,它们会回到0或null(或其他)?
这是我刚上传的视频,可以更好地解释它,因为它有点令人困惑
http://www.youtube.com/watch?v=OHGj2z5SwQs
这是我正在使用的确切代码
int x = (int) e.getX();
int y = (int) e.getY();
int x2 = (int) e.getX(1);
int y2 = (int) e.getY(1);
boolean a1 = y > 0 && y < 200....etc
boolean a5 = etc...
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
x = 0;
y = 0;
x2 = 0;
y2 = 0;
////I'm setting the x and y values to 0 as suggested
text.setText("x:" + String.valueOf(x) + "y:" + String.valueOf(y));
//// This is so I can see the values on the screen
if (a1 && a5){
viewA1.setBackgroundColor(Color.GREEN);
viewA5.setBackgroundColor(Color.GREEN);
}
if (a1) {
viewA1.setBackgroundColor(Color.GREEN);
}
else if (a5) {
viewA5.setBackgroundColor(Color.GREEN);
}
break;
case MotionEvent.ACTION_POINTER_1_DOWN:
// /A Strummer
x = 0;
y = 0;
x2 = 0;
y2 = 0;
text1.setText("x:" + String.valueOf(x2) + "y:" + String.valueOf(y2));
if (a1 && a5){
viewA1.setBackgroundColor(Color.GREEN);
viewA5.setBackgroundColor(Color.GREEN);
}
if (a1) {
viewA1.setBackgroundColor(Color.GREEN);
}
else if (a5) {
viewA1.setBackgroundColor(Color.GREEN);
}
/////I have pretty much the same method for ACTION_UP & ACTION_POINTER_UP; I set x & y to 0.
如果您能想到任何事情,请告诉我。我尝试了你们解释的方法,看起来它会有所帮助,但它没有。
答案 0 :(得分:5)
我没有提示,但有一个工作解决方案和多点触控启动器的一些提示。我之前从未这样做过,所以在看到你的问题之后,我现在急切地想要多点触控是如何工作的。实际上,完成它是非常棘手的。这是startes的一个很好的例子(有四个角的视图,当其中一个指针触摸它时会着色 - 使用一个最多五个指针/指针)。因此,我将根据下面进一步发布的工作解决方案对其进行解释。
基本上它的工作方式如下:getPointerCount()正在为您提供当前触摸指针的数量。 getX(i)和getY(i)正在为您提供相应的坐标。但这一部分很棘手:指针0,1,2正在触摸,你举起1(第二根手指),现在你有0,1。这是getPointerId(1)将返回2的点,因为这仍然是你的指针2这是触摸但它现在在位置1而不是2.您只有一次机会对该变化作出反应,即ACTION_POINTER_UP被触发时。在此操作上,ACTION_POINTER_DOWN getActionIndex()将返回指针事件列表中添加或删除的指针的操作指针 position 。否则它总是返回0,这是没有人告诉你的。
结论是你无法实际跟踪哪个指针正在移动(这就是移动时getActionIndex()返回0的原因)。这似乎是荒谬的逻辑,因为为每个手指触发5个单独的事件是愚蠢的。但是从多点触控开始这是一个非常重要的事实; - )
这是一个例子。 coordsX
和coordsY
将跟踪指针坐标,如果位置2包含有效坐标,则表示这是您的第三根手指仍然触摸屏幕,无论您是否抬起其他手指。这可能与这种情况无关,但这是一个在其他多点触控实现中仍然有用的事实。
public class MultitouchView extends LinearLayout {
// we have a maximum of 5 pointer
// we will set Integer.MIN_VALUE if the pointer is not present
private int [] coordsX = new int [5];
private int [] coordsY = new int [5];
// add 4 views with a certain gravity so they are placed in corners
public MultitouchView(Context context, AttributeSet attrs) {
super(context, attrs);
// initialize as not present
Arrays.fill(coordsX, Integer.MIN_VALUE);
Arrays.fill(coordsY, Integer.MIN_VALUE);
/* the layout is inflated from a XML file and is basically this one:
* all subviews are matching / filling parent and have a weight of 1
* <LinearLayout vertical>
* <LinearLayout horizontal>
* <View /><View />
* </LinearLayout>
* <LinearLayout horizontal>
* <View /><View />
* </LinearLayout>
* </LinearLayout>
*/
}
@Override
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_CANCEL) {
// every touch is going to be canceled, clear everything
Arrays.fill(coordsX, Integer.MIN_VALUE);
Arrays.fill(coordsY, Integer.MIN_VALUE);
} else {
// track all touches
for (int i = 0; i < e.getPointerCount(); i++) {
int id = e.getPointerId(i);
if (e.getActionIndex() == i
&& (e.getActionMasked() == MotionEvent.ACTION_POINTER_UP
|| e.getActionMasked() == MotionEvent.ACTION_UP)) {
// pointer with this id is about to leave, clear it
coordsX[id] = coordsY[id] = Integer.MIN_VALUE;
} else {
// update all other pointer positions
coordsX[id] = (int) e.getX(i);
coordsY[id] = (int) e.getY(i);
}
}
}
int hw = getWidth() / 2;
int hh = getHeight() / 2;
// first no corner is touched
boolean topLeft = false, topRight = false, bottomRight = false, bottomLeft = false;
// check if any of the given pointer is touching a certain corner
for (int i = 0; i < coordsX.length; i++) {
// pointer is not active (anymore)
if (coordsX[i] == Integer.MIN_VALUE || coordsY[i] == Integer.MIN_VALUE) {
continue;
}
topLeft = topLeft || coordsX[i] < hw && coordsY[i] < hh;
topRight = topRight || coordsX[i] > hw && coordsY[i] < hh;
bottomRight = bottomRight || coordsX[i] > hw && coordsY[i] > hh;
bottomLeft = bottomLeft || coordsX[i] < hw && coordsY[i] > hh;
}
// set the result we have now to the views represnting the corners
((ViewGroup) getChildAt(0)).getChildAt(0).setBackgroundColor(topLeft ? 0xffffff00 : 0x0);
((ViewGroup) getChildAt(0)).getChildAt(1).setBackgroundColor(topRight ? 0xffff0000 : 0x0);
((ViewGroup) getChildAt(1)).getChildAt(0).setBackgroundColor(bottomLeft ? 0xff00ff00 : 0x0);
((ViewGroup) getChildAt(1)).getChildAt(1).setBackgroundColor(bottomRight ? 0xff0000ff : 0x0);
return true;
}
}
答案 1 :(得分:1)
您应该使用'motionEvent.getPoinerCount()'来检查触摸点的数量。