Bas*_*vis 8 android android-layout
我希望我的GestureOverlayView可以检测纯粹的水平和垂直手势.
来自http://android-developers.blogspot.com/2009/10/gestures-on-android-16.html
"orientation"表示下方视图的滚动方向.在这种情况下,列表垂直滚动,这意味着任何水平手势(如action_delete)都可以立即被识别为手势.以垂直笔划开头的手势必须至少包含一个要识别的水平分量.换句话说,简单的垂直线不能被识别为手势,因为它会与列表的滚动冲突.
看起来像一个catch-22,如果android:orientation设置为vertical,我只能进行水平滑动,如果android:orientation设置为水平,我只能进行垂直滑动.我怎样才能解决这个问题?
我在黑客马拉松遇到了这个问题.使用GestureOverlayView无法解决问题,因此您必须使用运动事件.然而,GestureOverlayView可以使用稍微更"详细"的手势,就像圆圈一样.我注意到的是,GestureOverlayView最适合封闭的数字.那么,它就是motionEvent传感器.我在这里找到了代码.我修改了Gav对垂直滑动的回答.虽然没有测试过,但积极的认为应该有效.
这是一个例子:
public class SelectFilterActivity extends Activity implements OnClickListener
{
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;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... */
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
// downward swipe
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY)
Toast.makeText(SelectFilterActivity.this, "Downward Swipe", Toast.LENGTH_SHORT).show();
else if (Math.abs(e2.getY() - e1.getY()) > SWIPE_MAX_OFF_PATH && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY)
Toast.makeText(SelectFilterActivity.this, "Upward Swipe", Toast.LENGTH_SHORT).show();
// right to left swipe
else if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
现在将其添加到您想要手势检测的视图中,
// Do this for each view added to the grid
imageView.setOnClickListener(SelectFilterActivity.this);
imageView.setOnTouchListener(gestureListener);
Run Code Online (Sandbox Code Playgroud)
欢呼加密为代码!
所有你需要做的,是将GestureStrokeAngleThreshold设置为接近90的值(我使用90f)
角度threashold的默认值是40.0f,因为您将跳过简单的垂直手势
| 归档时间: |
|
| 查看次数: |
5752 次 |
| 最近记录: |