开发者

Android multi-touch support

I wonder if is the Android multi-touch support reliable? I've read it suffers from some problems.

I also wonder, how can I define custom multi-touch gestures? Like: 3 fingers rotate or 3 fingers stay static and fourth is movin开发者_如何学JAVAg.

I've come across some resources (Gestures or MotionEvent on developer.android.com) but nothing states it clearly.

Regards,

Zdenek


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜