平滑滚动带惯性和边缘性/折返平滑、惯性、边缘

由网友(2B範er的小青年)分享简介:我通过触摸拖动和缩放多点触控的自定义视图实现基本的滚动。这个效果很好,但现在我想补充一些高级功能。I implemented basic scrolling via touch dragging and multitouch zooming for a custom view. This works well, bu...

我通过触摸拖动和缩放多点触控的自定义视图实现基本的滚动。这个效果很好,但现在我想补充一些高级功能。

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.

例如,在谷歌地图应用程序,当你在屏幕上拖拉,在您停止拖动它仍然会继续动一动(惯性)。而一些浏览器(如iPad的Safari浏览器),让您进一步拖动屏幕比网站的可见区域,但随后屏幕上会迅速弹回至该网站的边缘。

For example, in the Google Maps application, when you drag around the screen, after you stop dragging it will still continue to move a bit (inertia). And some browsers (e.g. IPad Safari) allow you to drag the screen further than the visible area of the website, but then the screen will quickly snap back to the edge of the website.

我现在想实现类似的东西,但要做到这一点,我需要在的事件发生在定期触摸进行动画来改变激活画面区域的。我该怎么办呢?

I'd now like to implement something similar, but to do that I need to change the active screen region after the touch events happened in regular intervals to perform the animation. How can I do that?

推荐答案

使用一个OnGestureListener.为了提供平滑滚动,创建一个滚动条(在你的自定义视图)。当手势侦听器检测一扔事件,设置滚动条了。然后,覆盖您的自定义视图的computeScroll()方法。

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;
    }
  }
阅读全文

相关推荐

最新文章