Java 根据速度或其他变量向左、向右、向上和向下滑动
我有一个从简单的手势扩展而来的类,我正在使用onfling方法:Java 根据速度或其他变量向左、向右、向上和向下滑动,java,android,velocity,gesture,swipe,Java,Android,Velocity,Gesture,Swipe,我有一个从简单的手势扩展而来的类,我正在使用onfling方法: class MyGestureListener extends GestureDetector.SimpleOnGestureListener{ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-ge
class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
float e1_X = e1.getX();
float e1_Y = e1.getY();
float e2_X = e1.getX();
float e2_Y = e2.getY();
if(velocityX > 0 && velocityX > velocityY){
text.setText("Swipe left");
}else if(velocityX < 0 && velocityX < velocityY){
text.setText("Swipe right");
}else if(velocityY < 0 && velocityX > velocityY){
text.setText("Swipe down");
}else if(velocityY > 0 && velocityX < velocityY){
text.setText("Swipe up");
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
类MyGestureListener扩展了GestureDetector.SimpleOnGestureListener{
@凌驾
公共布尔onFling(运动事件e1、运动事件e2、浮点速度X、,
浮动速度y){
//TODO自动生成的方法存根
浮点e1_X=e1.getX();
浮点e1_Y=e1.getY();
float e2_X=e1.getX();
float e2_Y=e2.getY();
如果(velocityX>0&&velocityX>velocityY){
text.setText(“向左滑动”);
}else if(速度x<0&&velocityX我知道这取决于某些角度,但我做不到,我试过使用velocityX,velocityY,只有当你精确地操作它时,它才会工作。但我想要的是一个“错误”的角度:例如,如果你沿对角线向上向右滑动,我需要选择哪种方式是好的。你应该检查速度和距离。下面是水平滑动检测器的一个示例。您可以以相同的方式添加垂直检测
public class HSwipeDetector extends SimpleOnGestureListener {
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
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { return false; }
/* positive value means right to left direction */
final float distance = e1.getX() - e2.getX();
final boolean enoughSpeed = Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY;
if(distance > SWIPE_MIN_DISTANCE && enoughSpeed) {
// right to left swipe
onSwipeLeft();
return true;
} else if (distance < -SWIPE_MIN_DISTANCE && enoughSpeed) {
// left to right swipe
onSwipeRight();
return true;
} else {
// oooou, it didn't qualify; do nothing
return false;
}
}
protected void onSwipeLeft() {
// do your stuff here
}
protected void onSwipeRight() {
// do your stuff here
}
}
public类HSwipeDetector扩展SimpleNogestureListener{
专用静态最终整数滑动距离=120;
专用静态最终整数滑动路径=250;
专用静态最终整数滑动\u阈值\u速度=200;
@凌驾
公共布尔onFling(最终运动事件e1、最终运动事件e2、最终浮点速度yx、最终浮点速度yy){
if(Math.abs(e1.getY()-e2.getY())>swip_MAX_OFF_PATH){return false;}
/*正值表示从右向左的方向*/
最终浮动距离=e1.getX()-e2.getX();
最终布尔值enoughSpeed=Math.abs(velocityX)>滑动阈值速度;
如果(距离>滑动\最小\距离和足够速度){
//从右向左滑动
onswipleft();
返回true;
}否则如果(距离<-滑动距离和足够速度){
//从左向右滑动
onswipright();
返回true;
}否则{
//哦,它不合格;什么也不做
返回false;
}
}
受保护的void onswipleft(){
//在这里做你的事
}
受保护的void onswipright(){
//在这里做你的事
}
}
是的,我知道,但我希望在设置这些条件之前,上下滑动到。这对我很有帮助,但这并不是我想要的答案。在对角滑动的情况下,将VelocityX与VelocityY进行比较与将distanceX与distanceY进行比较是一样的(因为速度来自距离)。速度是衡量轻击动作能量的尺度。为了完成你的任务,你需要分析距离,并将速度作为一个阈值,以了解用户的移动是否足够快,是否符合滑动的条件。好的,所以我需要使用的是滑动路径。此变量用于查看用户是垂直还是水平滑动。我在@JBM上发现了一些东西:你能告诉我如何计算“滑动阈值\速度”值吗?只要找出最适合你的值。就我而言是200。在您的情况下,根据所需的响应级别,它可以是更少或更多。试着自己想想。顺便说一句,在计算之前把像素转换成倾角是个好主意。这将使其在不同密度的不同屏幕上保持稳定。