Smooth scrolling with inertia and edge resistance/snapback

后端 未结 2 651
栀梦
栀梦 2021-02-01 11:08

I implemented basic scrolling via touch dragging and multitouch zooming for a custom view. This works well, but now I\'d like to add some advanced features.

For example,

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 12:02

    Use a OnGestureListener. To provide smooth scroll, create a scroller (in your custom view). When the gesture listener detects a fling event, set the scroller up. Then, override your custom view's computeScroll() method.

    Check this example to know how to implement it.

    int lastX;
    int lastY;
    Scroller scroller;    
    @Override
    public void computeScroll() {
      if (scroller.computeScrollOffset()) {
        if (!scrolledLastFrame) {
          lastX = scroller.getStartX();
          lastY = scroller.getStartY();
        }
    
        int dx = scroller.getCurrX() - lastX;
        int dy = scroller.getCurrY() - lastY;
    
        lastX = scroller.getCurrX();
        lastY = scroller.getCurrY();
    
        doScroll(dx, dy);
        scrolledLastFrame = true;
      } else {
        scrolledLastFrame = false;
      }
    
    }    
    
    public void doFling(int startX, int startY, int velocityX, int velocityY,
        int minX, int maxX, int minY, int maxY) {
      scroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY);
      invalidate();
    }
    
    public void doScroll(int dx, int dy) {
      currentX+=dx;
      currentY+=dy;
    
      invalidate();
    }
    
    private class ProgramGestureListener extends SimpleOnGestureListener {
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
    
          doScroll(distanceX, distanceY);
          return true;
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
          int max_left = getMaxHorizontalScroll();
          int max_top = getMaxVerticalScroll();
          int min_left = getMinHorizontalScroll();
          int min_top = getMinVerticalScroll();
    
          int startX = getCurrentHorizontalScroll();
          int startY = getCurrentVerticalScroll();
    
          doFling(startX, startY, (int) -velocityX, (int) -velocityY,
              min_left, max_left, min_top, max_top);
    
          return true;
        }
      }
    

提交回复
热议问题