android - Android 中 ImageView 上的滑动(fling)手势识别问题

标签 android gesture-recognition swipe-gesture

我正在尝试在我的应用程序的 ImageView 上使用简单的滑动/滑动手势识别器。但是,我什么也没看到。任何人都可以看看代码并让我知道我在这里缺少什么吗?谢谢。

注意 1:从我的调试中,我可以看到执行确实进入了 GestureListener 内部。但是,它会从那里返回 false。

代码:

public class ViewSuccess extends Activity {

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;
ImageView movieFrame;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);



    imageLoader=new ImageLoader(getApplicationContext());




    movieFrame = (ImageView) findViewById(R.id.widget32);
    movieFrame.setImageResource(R.drawable.sample);





 // Gesture detection
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

    movieFrame.setOnTouchListener(gestureListener);


class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        System.out.println("in gesture recognizer !!!");

        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) {
                Toast.makeText(ViewSuccess.this, "Left Swipe", Toast.LENGTH_SHORT).show();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(ViewSuccess.this, "Right Swipe", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}
}

最佳答案

开始工作....我们需要将以下方法添加到 SimpleOnGestureListener 类中

 @Override
    public boolean onDown(MotionEvent e)
    {
        return true;
    }

关于android - Android 中 ImageView 上的滑动(fling)手势识别问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535612/

相关文章:

java - 如何从对象 id 的 String[] 中保存 List<ParseUser>

Android:前置摄像头的手势识别

java - 使用手势覆盖检测 Android 字母手势

ios - 如果用户在负方向平移 x 和 y,UIPanGestureRecognizer 不会切换到状态 "End"或 "Cancel"

android - 程序类型已经存在:org.tensorflow.Graph $ Reference

java - android:每次点击播放不同的声音

android - 以编程方式获取尺寸

c# - UWP 上的水平滑动手势

flutter - 在 flutter 中向上滑动导航到主屏幕

javascript - 如何在JQM网站中使用touchSwipe事件来滑动div