1
嗨在我的應用程序中,我有一個位置,我想要一個手指在上半部分來控制頂部的東西和底部的一個手指,以同時控制底部的東西。所以我試圖實施多點觸控,但我似乎無法得到正確的ACTION_MOVE。當我在屏幕上移動兩個手指時,它只會首先移動觸摸屏幕一側的對象。我想知道爲什麼會出現這種情況?我的繼承人代碼:Android多點觸控 - ACTION_MOVE多指針
public boolean onTouch(View v, MotionEvent event){
//needs multitouch
if(checkInGame()){
int pointerIndex = 0;
if(event.getY() < this.getMeasuredHeight()/2){
pointerId1 = event.getPointerId(event.getActionIndex());
pointerIndex = event.findPointerIndex(pointerId1);
}else{
pointerId2 = event.getPointerId(event.getActionIndex());
pointerIndex = event.findPointerIndex(pointerId2);
}
switch(event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_MOVE:{
int numPointers = event.getPointerCount();
for (int i = 0; i < numPointers; i++){
if(event.getY(i) < this.getMeasuredHeight() /2){
Log.d("Touch","Move1");
int moveX = 0;
int moveY = 0;
moveX = (int) event.getX(i) - sX;
moveY = (int) event.getY(i) - sY;
ship1.setLoc(moveX, moveY);
sX = sX + moveX;
sY = sY + moveY;
}else{
Log.d("Touch","Move2");
int moveX = 0;
int moveY = 0;
moveX = (int) event.getX(i) - sX2;
moveY = (int) event.getY(i) - sY2;
ship2.setLoc(moveX, moveY);
sX2 = sX2 + moveX;
sY2 = sY2 + moveY;
}
return true;
}
}
case MotionEvent.ACTION_DOWN: {
if(event.getY(pointerIndex) < this.getMeasuredHeight() /2){
Log.d("Touch","Top1");
sX = (int) event.getX(pointerIndex);
sY = (int) event.getY(pointerIndex);
}else{
Log.d("Touch","Bottom1");
sX2 = (int) event.getX(pointerIndex);
sY2 = (int) event.getY(pointerIndex);
}
return true;
}
case MotionEvent.ACTION_POINTER_DOWN:{
if(event.getY(pointerIndex) < this.getMeasuredHeight() /2){
Log.d("Touch","Top2");
sX = (int) event.getX(pointerIndex);
sY = (int) event.getY(pointerIndex);
}else{
Log.d("Touch","Bottom2");
sX2 = (int) event.getX(pointerIndex);
sY2 = (int) event.getY(pointerIndex);
}
return true;
}
}
非常感謝 – MikeT