Android: How to detect when a scroll has ended

后端 未结 14 889
误落风尘
误落风尘 2020-11-27 10:54

I am using the onScroll method of GestureDetector.SimpleOnGestureListener to scroll a large bitmap on a canvas. When the scroll has ended I want to redraw the bitmap in case

14条回答
  •  青春惊慌失措
    2020-11-27 11:51

    Coming back to this after a few months I've now followed a different tack: using a Handler (as in the Android Snake sample) to send a message to the app every 125 milliseconds which prompts it to check whether a Scroll has been started and whether more than 100 milliseconds has elapsed since the last scroll event.

    This seems to work pretty well, but if anyone can see any drawbacks or possible improvements I should be grateful to hear of them.

    The relevant the code is in the MyView class:

    public class MyView extends android.view.View {
    
    ...
    
    private long timeCheckInterval = 125; // milliseconds
    private long scrollEndInterval = 100;
    public long latestScrollEventTime;
    public boolean scrollInProgress = false;
    
    public MyView(Context context) {
        super(context);
    }
    
    private timeCheckHandler mTimeCheckHandler = new timeCheckHandler();
    
    class timeCheckHandler extends Handler{
    
            @Override
            public void handleMessage(Message msg) {
            long now = System.currentTimeMillis();
            if (scrollInProgress && (now>latestScrollEventTime+scrollEndInterval)) {
                        scrollInProgress = false;
    

    // Scroll has ended, so insert code here

    // which calls doDrawing() method

    // to redraw bitmap re-centred where scroll ended

                        [ layout or view ].invalidate();
            }
            this.sleep(timeCheckInterval);
            }
    
            public void sleep(long delayMillis) {
                this.removeMessages(0);
                sendMessageDelayed(obtainMessage(0), delayMillis);
                }
        }
    }
    
    @Override protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
    

    // code to draw large buffer bitmap onto the view's canvas // positioned to take account of any scroll that is in progress

    }
    
    public void doDrawing() {
    

    // code to do detailed (and time-consuming) drawing // onto large buffer bitmap

    // the following instruction resets the Time Check clock // the clock is first started when // the main activity calls this method when the app starts

            mTimeCheckHandler.sleep(timeCheckInterval);
    }
    

    // rest of MyView class

    }

    and in the MyGestureDetector class

    public class MyGestureDetector extends SimpleOnGestureListener {
    
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
    
        [MyView].scrollInProgress = true;
            long now = System.currentTimeMillis();  
        [MyView].latestScrollEventTime =now;
    
        [MyView].scrollX += (int) distanceX;
        [MyView].scrollY += (int) distanceY;
    

    // the next instruction causes the View's onDraw method to be called // which plots the buffer bitmap onto the screen // shifted to take account of the scroll

        [MyView].invalidate();
    
    }
    

    // rest of MyGestureDetector class

    }

提交回复
热议问题