我想知道
Android多点触控支持是否可靠?我读过它有一些问题.
我也想知道,如何定义自定义多点触控手势?喜欢:3个手指旋转或3个手指保持静止,第4个手指移动.
我遇到过一些资源(developer.android.com上的Gestures或MotionEvent),但没有任何说明.
问候,
兹德涅克
解决方法
我在API中进行了挖掘并找到了一种执行手势的方法,例如捏/反向捏,所以我相信你描述的手势是可能的 – 它只需要弄清楚如何编码它们.下面我贴了一个我实现的反向夹点的例子.我希望捏只能在水平方向上进行注册.它不是非常干净或可重复使用的代码,但它应该可以帮助您前进.它适用于Android 2.0.x.我读过多点触控可能在早期版本上有问题.这个例子是我在一个活动的onTouchEvent中调用的一个类,它将事件转发给类.
public class HorizontalReversePinchDetector { public boolean onTouchEvent(MotionEvent e) { int pointerCount = e.getPointerCount(); if(pointerCount != 2) { Log.d(GESTURE,"not pinching - exactly 2 fingers are needed but have " + pointerCount); clearPinch(); return false; } int firstIndex = e.getX(0) < e.getX(1) ? 0: 1; int secondIndex = e.getX(0) < e.getX(1) ? 1: 0; Finger currentLeftFinger = new Finger(e.getX(firstIndex),e.getY(firstIndex)); Finger currentRightFinger = new Finger(e.getX(secondIndex),e.getY(secondIndex)); float yDifference = Math.abs(currentLeftFinger.getY() - currentRightFinger.getY()); if(yDifference > 80) { Log.d(GESTURE,"not pinching - fingers too vertically-oriented"); clearPinch(); return false; } if(initialLeftFinger == null) { initialLeftFinger = currentLeftFinger; initialRightFinger = currentRightFinger; Log.d(GESTURE,"not pinching,but might be starting a pinch..."); return false; } float leftFingerDistance = initialLeftFinger.getX() - currentLeftFinger.getX(); float rightFingerDistance = currentRightFinger.getX() - initialRightFinger.getX(); float xDistanceBetweenFingers = Math.abs(currentLeftFinger.getX() - currentRightFinger.getX()); if(xDistanceBetweenFingers < minimumDistanceBetweenFingers) { Log.d(GESTURE,"pinching,but fingers are not far enough apart..."); return true; } if(leftFingerDistance < minimumDistanceForEachFinger) { Log.d(GESTURE,but left finger has not moved enough..."); return true; } if(rightFingerDistance < minimumDistanceForEachFinger) { Log.d(GESTURE,but right finger has not moved enough..."); return true; } pinchCompleted(); return true; } private void pinchCompleted() { Log.d(GESTURE,"pinch completed"); if(pinchListener != null) pinchListener.onPinch(); clearPinch(); } public static interface OnPinchListener { void onPinch(); } private void clearPinch() { initialLeftFinger = null; initialRightFinger = null; } public void setPinchListener(OnPinchListener pinchListener) { this.pinchListener = pinchListener; } private static class Finger { private Finger(float x,float y) { this.x = x; this.y = y; } public float getX() { return x; } public float getY() { return y; } private float x; private float y; } private Finger initialLeftFinger; private Finger initialRightFinger; private OnPinchListener pinchListener; private static final float minimumDistanceForEachFinger = 30; private static final float minimumDistanceBetweenFingers = 50; }
关于可靠性,该代码完全可靠.同样,仅适用于Android 2.0.x.我没有在其他版本的平台上测试它.