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

标签 android android-canvas smooth-scrolling

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

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

一定有更好的方法:有人可以帮忙吗?

最佳答案

这是我解决问题的方法。希望这会有所帮助。

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

关于Android:如何检测滚动何时结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2089552/

相关文章:

android - 在 Facebook 上通过 Intent 共享文本,而不使用 Facebook sdk

javascript - Slick Carousel 平滑滚动

ios - 使用来自光盘的数千张图像实现 Photo Grid 的平滑滚动

jquery - 通过使用 jQuery 单击 anchor 来平滑滚动元素

java - 如何按即将到来的生日顺序获取联系人?

android - 使用安卓应用程序控制外设

Android,将 WebView 与 loadData 一起使用

android - 如何在android中动画绘制圆弧

Android - 将 View 设置在使用 Canvas 绘制的项目之上

android - 如何在 Android 上触摸和拉动文本绕一个圆圈旋转?