Android OnGestureListener Fling没有检测到

前端之家收集整理的这篇文章主要介绍了Android OnGestureListener Fling没有检测到前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在屏幕的一个屏幕中检测到运动.我正在使用以下代码.
public class MyinfoActivity extends Activity implements OnGestureListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ..........
        ..........
        gestureScanner = new GestureDetector(this);
        resBlock = (TableRow) findViewById(R.id.ResBlock);
        gestureScanner = new GestureDetector(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me){
        Log.d(null,"Touch");
        if (gestureScanner.onTouchEvent(me))
            return gestureScanner.onTouchEvent(me);
        else
            return false;
    }

    @Override
    public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY) {
        Log.d(null,"Fling");
        ............
        ............
    }

    @Override
    public boolean onDown(MotionEvent arg0) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {}

    @Override
    public boolean onScroll(MotionEvent e1,float distanceX,float distanceY) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {}

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}

它正在检测到TouchEvent,但它并没有检测到任何甩动.我的代码有什么问题?

解决方法

我使用以下代码解决了这个问题.
public class MyinfoActivity extends Activity {
    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myinfotrackerinner);
        gestureScanner = new GestureDetector(this,simpleOnGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureScanner.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener =
                       new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent event) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1,MotionEvent event2,float velocityY) {
            Log.d(null,"Fling");
            int dx = (int) (event2.getX() - event1.getX());
            // don't accept the fling if it's too short
            // as it may conflict with a button push
            if (Math.abs(dx) > MAJOR_MOVE 
                       && Math.abs(velocityX) > Math.abs(velocityY)) {
                if (velocityX > 0) {
                    moveGraph("L");
                } else {
                    moveGraph("R");
                }
                return true;
            } else {
                return false;
            }
        }
    };
}
原文链接:https://www.f2er.com/android/313143.html

猜你在找的Android相关文章