【发布时间】:2011-10-27 17:41:24
【问题描述】:
我想知道如何为 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.
如果你能想到什么,请告诉我。我尝试了你们解释的方法,似乎有帮助,但没有。
【问题讨论】:
标签: android