Android:Horizo​​ntalScrollView smoothScroll动画时间

som*_*min 27 android smooth-scrolling horizontalscrollview

我有一个情况,我使用水平滚动视图与图像和使用按钮平滑滚动到不同的图像位置.现在它工作正常我只是想知道是否有人知道无论如何减慢平滑滚动方法,即有更长的动画时间?目前,这种捕捉很快就会发生.

也许通过覆盖smoothscroll,我试图搜索这个/例子,但没有运气.

那么任何想法?

谢谢,

Sai*_*kat 55

怎么样:

ObjectAnimator animator=ObjectAnimator.ofInt(yourHorizontalScrollView, "scrollX",targetXScroll );
animator.setDuration(800);
animator.start();
Run Code Online (Sandbox Code Playgroud)

  • 这应该是选定的答案.除了要求API 11或更高版本,它是完美的解决方案.(在我的库代码中,我已经退回到在API 11之前使用`smoothScrollTo`). (4认同)

Lum*_*mis 14

这是一种方式,对我来说效果很好:

    new CountDownTimer(2000, 20) { 

        public void onTick(long millisUntilFinished) { 
            hv.scrollTo((int) (2000 - millisUntilFinished), 0); 
        } 

        public void onFinish() { 

        } 
     }.start();
Run Code Online (Sandbox Code Playgroud)

所以这里水平滚动视图(hv)在从位置0到2000的两秒内移动,或者如果小于2000px则移动到视图的末尾.易于调整......


小智 13

子类Horizo​​ntalScrollView,使用反射来访问mScrollerHorizo​​ntalScrollView中的私有字段.当然,如果底层类更改了字段名称,它将会中断,它默认返回原始滚动实现.

通话会myScroller.startScroll(scrollX, getScrollY(), dx, 0, 500);更改滚动速度.

private OverScroller myScroller;     

private void init()
{
    try
    {
        Class parent = this.getClass();
        do
        {
            parent = parent.getSuperclass();
        } while (!parent.getName().equals("android.widget.HorizontalScrollView"));

        Log.i("Scroller", "class: " + parent.getName());
        Field field = parent.getDeclaredField("mScroller");
        field.setAccessible(true);
        myScroller = (OverScroller) field.get(this);

    } catch (NoSuchFieldException e)
    {
        e.printStackTrace();
    } catch (IllegalArgumentException e)
    {
        e.printStackTrace();
    } catch (IllegalAccessException e)
    {
        e.printStackTrace();
    }
}

public void customSmoothScrollBy(int dx, int dy)
{
    if (myScroller == null)
    {
        smoothScrollBy(dx, dy);
        return;
    }

    if (getChildCount() == 0)
        return;

    final int width = getWidth() - getPaddingRight() - getPaddingLeft();
    final int right = getChildAt(0).getWidth();
    final int maxX = Math.max(0, right - width);
    final int scrollX = getScrollX();
    dx = Math.max(0, Math.min(scrollX + dx, maxX)) - scrollX;

    myScroller.startScroll(scrollX, getScrollY(), dx, 0, 500);
    invalidate();
}

public void customSmoothScrollTo(int x, int y)
{
    customSmoothScrollBy(x - getScrollX(), y - getScrollY());
}
Run Code Online (Sandbox Code Playgroud)


小智 -5

看看http://developer.android.com/reference/android/widget/Scroller.html

滚动的持续时间可以在构造函数中传递,并指定滚动动画应该花费的最长时间