Android的支持多点触控 [英] Android multi-touch support
问题描述
不知是Android支持多点触控可靠吗?我读过一些经常出现问题。
I wonder if is the Android multi-touch support reliable? I've read it suffers from some problems.
我也想知道,我怎么可以定义自定义多点触摸手势?像:3个手指旋转或3个手指停留静态和第四是移动
I also wonder, how can I define custom multi-touch gestures? Like: 3 fingers rotate or 3 fingers stay static and fourth is moving.
我已经遇到了一些资源(手势或MotionEvent上developer.android.com),但没有规定清楚。
I've come across some resources (Gestures or MotionEvent on developer.android.com) but nothing states it clearly.
问候,
兹德涅克
推荐答案
我在API中挖了一圈,发现一个方法来执行的手势,比如开合/反向捏,所以我相信你所描述的手势是可能的 - 它只是需要搞清楚如何$ C C $他们了。下面我贴反掐我实现的一个例子。我想掐如果是水平为导向,以仅注册。它不是很干净或可重复使用code,但它应该帮助你前进。它的工作原理在Android 2.0.x的我读过多点触摸可能对早期版本的问题。这个例子是我从内部调用类的活动的的onTouchEvent
,将事件转发到类。
I've dug around in the API and found a way to perform gestures like pinch / reverse pinch, so I believe the gestures you describe are possible - it just takes figuring out how to code them up. Below I've pasted an example of a reverse pinch I implemented. I wanted the pinch to only register if it is horizontally-oriented. Its not very clean or reusable code but it should help you get moving forward. It works on Android 2.0.x. I've read multi-touch may have issues on earlier versions. The example is a class that I call from within an activity's onTouchEvent
, forwarding the event to the class.
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, "pinching, but left finger has not moved enough...");
return true;
}
if(rightFingerDistance < minimumDistanceForEachFinger) {
Log.d(GESTURE, "pinching, 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;
}
对于可靠性,这code已被完全可靠。同样,只有在Android 2.0.x的我还没有在其他版本的平台进行了测试。
Regarding reliability, this code has been completely reliable. Again, only on Android 2.0.x. I have not tested it on other versions of the platform.
这篇关于Android的支持多点触控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!