我将尝试在Android中处理多点触控,我的计划是在应用程序中使用两个手指。我知道如何使用一个触摸点,但我不确定如何使用多个触摸点。
以下是我的代码,在OnTouch方法中:
最后,Finger类:
以下是我的代码,在OnTouch方法中:
public boolean onTouch(View view, MotionEvent event) {
for(int i = 0; i < event.getPointerCount(); i++){
float x = event.getX(i);
float y = event.getY(i);
// using the data here...
//....
}
return true;
}
现在,当我获取每个点的x和y坐标后,如何获取发生的动作?我想使用event.getAction
,但它不接受诸如点索引之类的参数。如何获取每个触摸点的动作类型?
更新:
好的,现在我可以成功地管理和处理两个手指了。现在,我创建了一个简单的名为Finger
的类来处理每个触摸点。现在,每当我屏幕上有两个手指,我尝试移除其中之一,然后移动/删除另一个手指时,游戏就会崩溃!
OnTouch:
public boolean onTouch(View view, MotionEvent event) {
int pointerCount = event.getPointerCount();
if(pointerCount > 2){
pointerCount = 2;
System.out.println("too many fingers!");
} // since i want to handle only two fingers, every other finger will be ignored.
for (int i = 0; i < pointerCount; i++) {
float x = event.getX(i);
float y = event.getY(i);
int id = event.getPointerId(i);
int action = event.getActionMasked();
int actionIndex = event.getActionIndex();
if (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_POINTER_DOWN) {
if (fingers.get(i) == null)
fingers.set(i, new Finger(x, y, id));
}
if (fingers.get(i).type == Finger.SCREEN_FINGER) {
switch (action) {
case MotionEvent.ACTION_UP:
fingers.set(i, null);
System.out.println(id + " action_up!");
break;
case MotionEvent.ACTION_POINTER_UP:
fingers.set(i, null);
System.out.println(id + " pointer_up!");
break;
case MotionEvent.ACTION_MOVE:
fingers.get(i).setPos(x, y);
System.out.println(id + " action_move!, x: "+fingers.get(i).x+", y: "+fingers.get(i).y);
break;
default:
}
}else if (fingers.get(i).type == Finger.DPAD_FINGER) {
switch (action) {
case MotionEvent.ACTION_UP:
fingers.set(i, null);
System.out.println(id + " action_up! - dpad");
break;
case MotionEvent.ACTION_POINTER_UP:
fingers.set(i, null);
System.out.println(id + " pointer_up! - dpad");
break;
case MotionEvent.ACTION_MOVE:
fingers.get(i).setPos(x, y);
System.out.println(id + " action_move! - dpad, x: "+fingers.get(i).x+", y: "+fingers.get(i).y);
break;
default:
}
}
}
return true;
}
我创建了一个手指列表:
List fingers = new LinkedList();
在构造函数中,我放置了以下内容:
fingers.add(0, null);
fingers.add(1, null);
最后,Finger类:
public class Finger {
public final static int DPAD_FINGER = 0;
public final static int SCREEN_FINGER = 1;
public float x, y;
public int type;
public int id;
public Finger(float x, float y,int id) {
this.id = id;
checkType(x, y);
}
public void checkType(float x, float y) {
if(x>Dpad.x && x < Dpad.x+Dpad.Width && y> Dpad.y && y<Dpad.y+Dpad.Height){
System.out.println("inside DPAD");
type = DPAD_FINGER;
}else{
System.out.println("Outside DPAD");
type = SCREEN_FINGER;
}
}
public void setPos(float x, float y){
this.x = x;
this.y = y;
}
}
现在当我做完这些操作后,一切都很正常,但是当我移开一个手指并移动/删除另一个手指时,在LogCat中会出现以下错误:
05-18 15:22:03.812: E/InputEventReceiver(20124): Exception dispatching input event.
05-18 15:22:03.812: W/dalvikvm(20124): threadid=1: thread exiting with uncaught exception (group=0x41e00438)
05-18 15:22:03.822: E/AndroidRuntime(20124): FATAL EXCEPTION: main
05-18 15:22:03.822: E/AndroidRuntime(20124): java.lang.NullPointerException
05-18 15:22:03.822: E/AndroidRuntime(20124): at smellychiz.projects.ogc.util.ChizView$1.onTouch(ChizView.java:70)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.View.dispatchTouchEvent(View.java:7241)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
05-18 15:22:03.822: E/AndroidRuntime(20124): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2116)
05-18 15:22:03.822: E/AndroidRuntime(20124): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1469)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.app.Activity.dispatchTouchEvent(Activity.java:2477)
05-18 15:22:03.822: E/AndroidRuntime(20124): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2064)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.View.dispatchPointerEvent(View.java:7430)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3457)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3389)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4483)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4461)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4565)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4533)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4584)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.Choreographer.doFrame(Choreographer.java:523)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.os.Handler.handleCallback(Handler.java:615)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.os.Looper.loop(Looper.java:137)
05-18 15:22:03.822: E/AndroidRuntime(20124): at android.app.ActivityThread.main(ActivityThread.java:4950)
05-18 15:22:03.822: E/AndroidRuntime(20124): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 15:22:03.822: E/AndroidRuntime(20124): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 15:22:03.822: E/AndroidRuntime(20124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-18 15:22:03.822: E/AndroidRuntime(20124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-18 15:22:03.822: E/AndroidRuntime(20124): at dalvik.system.NativeStart.main(Native Method)
更新:
不再将手指重置并将它们设置为null,然后再重新设置它们,而是在手指抬起时将它们移除,手指按下时重新创建它们并在手指移动时进行更新。现在它完美地运作了!
更新....
最终问题并未得到解决,尽管应用程序不会崩溃,但当其中一个手指抬起时,它会像两个手指都抬起一样。
以下是onTouch
方法的当前代码:
public boolean onTouch(View view, MotionEvent event) {
int pointerCount = event.getPointerCount();
if(pointerCount > 2){
pointerCount = 2;
System.out.println("too many fingers!");
} // since i want to handle only two fingers, every other finger will be ignored.
for (int i = 0; i < pointerCount; i++) {
float x = event.getX(i);
float y = event.getY(i);
int id = event.getPointerId(i);
int action = event.getActionMasked();
int actionIndex = event.getActionIndex();
if (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_POINTER_DOWN) {
if (fingers.get(i) == null)
fingers.add(i, new Finger(x, y, id));
}
if (fingers.get(i).type == Finger.SCREEN_FINGER) {
switch (action) {
case MotionEvent.ACTION_UP:
fingers.remove(i);
System.out.println(id + " action_up!");
break;
case MotionEvent.ACTION_POINTER_UP:
fingers.remove(i);
System.out.println(id + " pointer_up!");
break;
case MotionEvent.ACTION_MOVE:
fingers.get(i).setPos(x, y);
System.out.println(id + " action_move!, x: "+fingers.get(i).x+", y: "+fingers.get(i).y);
break;
default:
}
}else if (fingers.get(i).type == Finger.DPAD_FINGER) {
switch (action) {
case MotionEvent.ACTION_UP:
fingers.remove(i);
System.out.println(id + " action_up! - dpad");
break;
case MotionEvent.ACTION_POINTER_UP:
fingers.remove(i);
System.out.println(id + " pointer_up! - dpad");
break;
case MotionEvent.ACTION_MOVE:
fingers.get(i).setPos(x, y);
System.out.println(id + " action_move! - dpad, x: "+fingers.get(i).x+", y: "+fingers.get(i).y);
break;
default:
}
}
}
所以每当其中一根手指抬起时,logCat 会写入好像两根手指都离开了屏幕。有什么想法吗?
Finger
来处理每个触点的ID并跟踪X和Y坐标。现在,我有一个问题,当我在屏幕上放置两个手指,并尝试移除其中一个手指,然后移动/删除另一个手指时,应用程序会崩溃!我将在问题中发布我的代码。 - Baruch