多点触控的处理
因此,我要做的就是围绕正在触摸的两个点绘制圆圈,当手指在屏幕上拖动时,让这些圆圈跟随每个手指。
但是,我遇到了一些我无法弄清楚的奇怪行为。因此,当我将两根手指放在屏幕上时,我的圆圈就没有问题了。当我用两根手指拖动时,一切都会按照我想要的方式进行。但是,如果我抬起第一根手指,所有内容都会停止重绘,并且仍在屏幕上的第二根手指将停止被跟踪。如果我把第一个手指放回去,一切都会恢复正常。我想我的两根手指一定处理不好。
谁能看到我做错了什么吗?
另外,如果我有两根手指在屏幕上,并且我抬起并触摸第二根手指,一切都会表现良好。只有在我有两个手指并且抬起第一个手指的情况下,我才会发现问题。
@Override
public boolean onTouchEvent(MotionEvent event) {
/*
* Log this touch event
*/
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("OnTouchEvent ACTION_" ).append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid " ).append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")" );
}
sb.append("[" );
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#" ).append(i);
sb.append("(pid " ).append(event.getPointerId(i));
sb.append(")=" ).append((int) event.getX(i));
sb.append("," ).append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";" );
}
sb.append("]" );
Log.d(TAG, sb.toString());
/*
* Track the touches that are on the screen
* Grab X,Y, and Size data
*/
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
if (actionCode == MotionEvent.ACTION_POINTER_DOWN) {
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = true;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = true;
}
}
else if (actionCode == MotionEvent.ACTION_POINTER_UP){
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = false;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = false;
}
}
if(actionCode == MotionEvent.ACTION_DOWN){
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = true;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = true;
}
}
else if (actionCode == MotionEvent.ACTION_UP){
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = false;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = false;
}
}
int pointCt = event.getPointerCount();
for(int i = 0; i < pointCt; i++){
int pid = event.getPointerId(i);
if((pid == 0) && p0Down){
curX1 = event.getX(pid);
curY1 = event.getY(pid);
size1 = event.getSize(pid);
}
if((pid == 0) && (p0Down == false)){
curX1 = -1;
curY1 = -1;
size1 = -1;
}
if((pid == 1) && p1Down){
curX2 = event.getX(pid);
curY2 = event.getY(pid);
size2 = event.getSize(pid);
}
if((pid == 1) && (p1Down == false)){
curX2 = -1;
curY2 = -1;
size2 = -1;
}
}
return true;
}
So all I'm trying to do is draw circles around two points that are being touched, and as the fingers drag along on the screen, have these circles follow each finger.
However, I'm getting some weird behavior that I can't figure out. So when I place two fingers on the screen I get my circles no problem. When I drag around with the two fingers everything works like I want it. However, if I lift the first finger, everything stops redrawing, and the second finger that is still on the screen stops being tracked. If I place the first finger back on, everything behaves good again. I figure I must be handling the two fingers poorly.
Can anyone see what I'm doing wrong?
Also, if I have two fingers on the screen, and I lift and touch the second finger, everything behaves well. It's only in the case where I have two fingers and I lift the first one do I see a problem.
@Override
public boolean onTouchEvent(MotionEvent event) {
/*
* Log this touch event
*/
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("OnTouchEvent ACTION_" ).append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid " ).append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")" );
}
sb.append("[" );
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#" ).append(i);
sb.append("(pid " ).append(event.getPointerId(i));
sb.append(")=" ).append((int) event.getX(i));
sb.append("," ).append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";" );
}
sb.append("]" );
Log.d(TAG, sb.toString());
/*
* Track the touches that are on the screen
* Grab X,Y, and Size data
*/
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
if (actionCode == MotionEvent.ACTION_POINTER_DOWN) {
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = true;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = true;
}
}
else if (actionCode == MotionEvent.ACTION_POINTER_UP){
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = false;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = false;
}
}
if(actionCode == MotionEvent.ACTION_DOWN){
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = true;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = true;
}
}
else if (actionCode == MotionEvent.ACTION_UP){
if (0 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p0Down = false;
}
if (1 == action >> MotionEvent.ACTION_POINTER_ID_SHIFT){
p1Down = false;
}
}
int pointCt = event.getPointerCount();
for(int i = 0; i < pointCt; i++){
int pid = event.getPointerId(i);
if((pid == 0) && p0Down){
curX1 = event.getX(pid);
curY1 = event.getY(pid);
size1 = event.getSize(pid);
}
if((pid == 0) && (p0Down == false)){
curX1 = -1;
curY1 = -1;
size1 = -1;
}
if((pid == 1) && p1Down){
curX2 = event.getX(pid);
curY2 = event.getY(pid);
size2 = event.getSize(pid);
}
if((pid == 1) && (p1Down == false)){
curX2 = -1;
curY2 = -1;
size2 = -1;
}
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用指针 ID 作为参数而不是索引来调用 MotionEvent#getX()/getY()/getSize()。
You're calling MotionEvent#getX()/getY()/getSize() with the pointer ID as the argument instead of the index.