gpt4 book ai didi

android:如何检测水平和垂直手势线

转载 作者:太空狗 更新时间:2023-10-29 15:17:53 25 4
gpt4 key购买 nike

我有一些要用线连接的对象。用户应该能够通过简单的线条手势来做到这一点。我使用 GestureOverlayView 并阅读了那篇文章 http://developer.android.com/resources/articles/gestures.html , 表示以下内容

orientation: indicates the scroll orientation of the views underneath. In this case the list scrolls vertically, which means that any horizontal gestures (like action_delete) can immediately be recognized as a gesture. Gestures that start with a vertical stroke must contain at least one horizontal component to be recognized. In other words, a simple vertical line cannot be recognized as a gesture since it would conflict with the list's scrolling.

这就是我的问题 - 我想画水平线和垂直线

现在我有一个 OnGesturePerfomedListener,它执行正常的手势识别,还有一个 GestureOverlayView.OnGestureListener,我在其中检测线条。但现在我想画一条虚线——也是垂直和水平的。如果我能像在 OnGesturePerformedListener 中那样获得完整的手势,而不是像在 onGestureListener 中那样获得虚线的每一笔画,那就容易多了。

有什么办法可以轻松解决这个问题吗?有没有一种方法,在手势完成时调用,即使它没有被识别?我还尝试使用 GestureDetector.OnGestureListener,我现在用它来检测 longPress,但它无助于解决该问题。

最佳答案

在其他线程中找到了类似的解决方案,可能对您(以及可能遇到此问题的任何其他人)有用。这很可能需要认真修改才能适应您的目的,但我认为它会让您接近:

//Swipe direction detection constants
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

//Gesture detection
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
}

//On gesture...
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return true;
}

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

try {
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//DO SOMETHING...
}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//DO SOMETHING...
}
// top to bottom swipe
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
//DO SOMETHING...
}
// bottom to top swipe
else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
//DO SOMETHING...
}
} catch (Exception e) {
return false;
}

return true;

}

@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

关于android:如何检测水平和垂直手势线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889118/

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