方法中的手势检测

时间:2015-09-25 14:33:35

标签: android gesture gesturedetector

嘿所有这些因此我遇到了一个问题,我需要能够在一个方法中使用手势检测器,所以我可以在所述方法中调用一个方法我会粘贴代码但基本上它无法到达我的内心声明因此引发了错误!

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);
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

然后当我打电话给班级使用手势探测器时:

public void notification() {
    final AchievementUnlocked achievementUnlocked = new AchievementUnlocked(MainActivity.this)
    achievementUnlocked.getAchievementView().setOnTouchListener(gestureListener);
    class MyGestureDetector extends GestureDetector.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;
                // left to right swipe
                if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                achievementUnlocked.dismiss();
                }
                // right to left swipe
                else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

如果我这样做,我在代码的这部分得到错误无法解析符号'MyGestureDetector':

 gestureDetector = new GestureDetector(this, new MyGestureDetector());

2 个答案:

答案 0 :(得分:1)

您无法在方法内声明常规Java类。将MyGestureDetector移到notification()方法之外。

答案 1 :(得分:0)

MyGestureDetector移到外面并将您在其中调用的方法(如achievementUnlocked.dismiss();)更改为静态