问题描述
我的提问:如何检测,当用户将其手指向上/向下VS左/右(以及我怎么知道这些群体他们的手指移动的方向)
My Question: How do I detect when a user moves their finger up/down vs left/right (and how do I know which direction of those groups their finger moved)?
我的情况:我想改变我的应用程序的亮度,当他们把他们的手指向上和向下(上=亮,向下=暗),我想活动之间的切换和/或根据他们的左/右轻扫意见。
My Situation: I want to change the brightness of my app when they move their finger up and down (up = brighter, down = darker), and I want to switch between activities and/or views based on their left/right swipe.
推荐答案
您只需要扩展SimpleOnGestureListener类,
You simply have to extend SimpleOnGestureListener class,
在你的类中声明这一点,
Declare this in your class,
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;
作为一个例子水平轻扫你可以看到下面的code,
As an example for horizontal swipe you can see the below code,
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
return false;
}
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onLeftSwipe();
}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onRightSwipe();
}
} catch (Exception e) {
}
return false;
}
}
您可以垂直刷卡目的也这样做。
You can do this similarly for vertical swipe purpose.
这篇关于如何检测左/右轻扫之间的方向和向上/向下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!