Android 多点触控支持

发布于 2024-08-29 19:07:46 字数 199 浏览 3 评论 0原文

不知道Android的多点触控支持可靠吗?我读过它遇到了一些问题。

我还想知道,如何定义自定义多点触控手势?例如:3 个手指旋转或 3 个手指保持静止,第四个手指移动。

我遇到过一些资源(developer.android.com 上的 Gestures 或 MotionEvent),但没有明确说明。

问候,

兹德内克

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 moving.

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

Regards,

Zdenek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2024-09-05 19:07:46

我已经深入研究了 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, "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;
}

从可靠性来看,这段代码是完全可靠的。再次强调,仅适用于 Android 2.0.x。我还没有在该平台的其他版本上进行过测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文