gpt4 book ai didi

java - 如何降低 android ScaleGestureDetector.SimpleOnScaleGestureListener 的灵敏度

转载 作者:行者123 更新时间:2023-11-29 04:32:57 26 4
gpt4 key购买 nike

我有一个自定义的 SwipeRefreshLayout,里面有一个自定义的 GridView。我想根据捏/缩放手势更改该 GridView 的列号。我已经成功地实现了它。问题是,秤太敏感了。

例如,我有第 3-5 列。缩放到 3 或 5 很容易,但要缩放到 4 很难,因为缩放本身太敏感了。

这是我自定义的 SwipeRefreshLayout 类

/**
* This class contains fix from http://stackoverflow.com/questions/23989910/horizontalscrollview-inside-swiperefreshlayout
*/
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {

private int mTouchSlop;
private float mPrevX;
private ScaleGestureDetector mScaleGestureDetector;
private ScaleListener mScaleListener;

public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

public void setScaleListener(Context context, ScaleListener scaleListener) {
this.mScaleListener = scaleListener;
mScaleGestureDetector = new ScaleGestureDetector(context, new MyOnScaleGestureListener());
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mScaleGestureDetector != null) {
mScaleGestureDetector.onTouchEvent(event);
}

switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
setEnabled(false);
if (mScaleListener != null) {
mScaleListener.onTwoFingerStart();
}
break;
case MotionEvent.ACTION_POINTER_UP:
setEnabled(true);
mPrevX = MotionEvent.obtain(event).getX();
if (mScaleListener != null) {
mScaleListener.onTwoFingerEnd();
}
break;
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
break;

case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);

if (xDiff > mTouchSlop) {
return false;
}
}

return super.onInterceptTouchEvent(event);
}

class MyOnScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

private static final String TAG = "MyOnScaleGestureListene";

@Override
public boolean onScale(ScaleGestureDetector detector) {
if (mScaleListener != null) {
// Too sensitive, must change to other approach
float scaleFactor = detector.getScaleFactor();
Log.d(TAG, "onScale: " + scaleFactor);
if (scaleFactor > 1F) {
mScaleListener.onScaleUp(scaleFactor);
} else if (scaleFactor < 1F) {
mScaleListener.onScaleDown(scaleFactor);
} else {
// no scale
}
}
return true;
}

@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}

@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
}

public interface ScaleListener {
void onTwoFingersStart();

void onTwoFingersEnd();

void onScaleUp(float scaleFactor);

void onScaleDown(float scaleFactor);
}
}

有什么办法可以降低ScaleGestureDetector.SimpleOnScaleGestureListener的灵敏度吗?如果没有办法,有没有办法解决?

这是一个显示问题的简短视频 https://www.youtube.com/watch?v=0MItDNZ_o4c

最佳答案

你应该有一个公差距离,这在Android世界中被称为“slop”。如果手势小于该容差,您应该忽略它。

private static final int SPAN_SLOP = 7;

...

@Override
public boolean onScale(@NonNull ScaleGestureDetector detector) {
if (gestureTolerance(detector)) {
// performing scaling
}
return true;
}

private boolean gestureTolerance(@NonNull ScaleGestureDetector detector) {
final float spanDelta = Math.abs(detector.getCurrentSpan() - detector.getPreviousSpan());
return spanDelta > SPAN_SLOP;
}

例如,您可以看到 @rallat 制作的展示开源应用程序.

enter image description here

在这里你可以找到source codepresentation at GOTO Copenhagen 2016 .

关于java - 如何降低 android ScaleGestureDetector.SimpleOnScaleGestureListener 的灵敏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43043176/

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