簡體   English   中英

根據速度或其他變量向左、向右、向上和向下滑動

[英]swipe left, right, up and down depending on velocity or other variables

我有一個 class 從簡單的手勢擴展而來,我正在使用 onfling 方法:

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);
    }
}

我知道這取決於某些角度,但我做不到,我嘗試使用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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM