alc*_*hra 5 android gesture android-gesture
我有一些想要用线连接的对象。用户应该能够通过简单的线条手势来完成此操作。我使用 GestureOverlayView 并阅读该文章http://developer.android.com/resources/articles/gestures.html,其中内容如下
orientation:指示下方视图的滚动方向。在这种情况下,列表垂直滚动,这意味着任何水平手势(如action_delete)都可以立即被识别为手势。以垂直笔画开始的手势必须至少包含一个水平分量才能被识别。换句话说,简单的垂直线不能被识别为手势,因为它会与列表的滚动冲突。
这就是我的问题 - 我想画水平和垂直的线
现在我有一个 OnGesturePerfomedListener,它执行正常的手势识别,另外还有一个 GestureOverlayView.OnGestureListener,我在其中检测线条。但现在我想画一条虚线 - 也垂直和水平。如果我能像 OnGesturePerformedListener 中那样获得完整的手势,而不是像 onGestureListener 中那样获得虚线的每一个笔划,那就容易多了。
我有什么想法可以轻松解决这个问题吗?是否有一种方法,在手势完成时被调用,即使它未被识别?我还尝试使用 GestureDetector.OnGestureListener,我现在用它来检测长按,但它无助于解决该问题。
在其他线程中找到了类似的解决方案,这可能对您(以及可能遇到此问题的任何其他人)有用。这很可能需要认真修改才能适应您的目的,但我认为它会让您接近:
//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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3098 次 |
| 最近记录: |