gpt4 book ai didi

java - Android 多点触控 - 具有多个指针的 ACTION_MOVE

转载 作者:行者123 更新时间:2023-11-30 04:12:25 26 4
gpt4 key购买 nike

您好,在我的应用程序中,我希望上半部分的手指可以控制顶部的东西,下半部分的手指可以同时控制底部的东西。所以我尝试实现多点触控,但我似乎无法正确设置 ACTION_MOVE。当我在屏幕上移动 2 个手指时,它只会移动首先触摸屏幕一侧的对象。我想知道为什么会这样?这是我的代码:

 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;
}


}

最佳答案

您的 return 语句在 for 循环内,因此它只会迭代一次。

关于java - Android 多点触控 - 具有多个指针的 ACTION_MOVE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10643718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com