平滑滚动带惯性和边缘性/折返 [英] Smooth scrolling with inertia and edge resistance/snapback

查看:161
本文介绍了平滑滚动带惯性和边缘性/折返的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

使用一个<一个href="http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html">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;
    }
  }

这篇关于平滑滚动带惯性和边缘性/折返的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆