Android:如何检测滚动何时结束

pre*_*bgg 65 android smooth-scrolling android-canvas

我正在使用GestureDetector.SimpleOnGestureListener的onScroll方法在画布上滚动一个大位图.当滚动结束时,我想重绘位图,以防用户想要进一步滚动...离开位图的边缘,但我看不到如何检测滚动何时结束(用户已抬起手指)从屏幕上).

e2.getAction()似乎总是返回值2,所以没有帮助.当压力似乎下降到0.13左右时,e2.getPressure似乎返回相当恒定的值(大约0.25)直到最后的onScroll调用.我想我可以发现这种压力减少,但这远非万无一失.

必须有更好的方法:请有人帮忙吗?

Ako*_* Cz 67

这是我解决问题的方法.希望这可以帮助.

// declare class member variables
private GestureDetector mGestureDetector;
private OnTouchListener mGestureListener;
private boolean mIsScrolling = false;


public void initGestureDetection() {
        // Gesture detection
    mGestureDetector = new GestureDetector(new SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            handleDoubleTap(e);
            return true;
        }

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

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // i'm only scrolling along the X axis
            mIsScrolling = true;                
            handleScroll(Math.round((e2.getX() - e1.getX())));
            return true;
        }

        @Override
        /**
         * Don't know why but we need to intercept this guy and return true so that the other gestures are handled.
         * https://code.google.com/p/android/issues/detail?id=8233
         */
        public boolean onDown(MotionEvent e) {
            Log.d("GestureDetector --> onDown");
            return true;
        }
    });

    mGestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if (mGestureDetector.onTouchEvent(event)) {
                return true;
            }

            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(mIsScrolling ) {
                    Log.d("OnTouchListener --> onTouch ACTION_UP");
                    mIsScrolling  = false;
                    handleScrollFinished();
                };
            }

            return false;
        }
    };

    // attach the OnTouchListener to the image view
    mImageView.setOnTouchListener(mGestureListener);
}
Run Code Online (Sandbox Code Playgroud)

  • 有意思......非常有帮助.但是在一次快速测试中,我似乎在投掷之前得到了一个补救事件.如果发生甩尾或滚动停止,我想做不同的事情.当我检测到修饰时,我怎么知道我是否要进行一次抛弃事件以便我不会执行正常的滚动结束功能? (2认同)