gpt4 book ai didi

安卓多点触控支持

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:38:49 28 4
gpt4 key购买 nike

不知Android的多点触控支持靠谱吗?我读过它存在一些问题。

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

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

问候,

兹德内克

最佳答案

我深入研究了 API 并找到了一种执行手势(例如捏合/反向捏合)的方法,因此我相信您描述的手势是可行的 - 只需弄清楚如何对它们进行编码。下面我粘贴了一个我实现的反向捏的例子。我希望捏合仅在水平方向时才注册。它不是很干净或可重用的代码,但它应该可以帮助您继续前进。它适用于 Android 2.0.x。我读过多点触控在早期版本上可能有问题。该示例是我从 Activity 的 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。我没有在其他版本的平台上测试过。

关于安卓多点触控支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2679473/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com