30

我有一种情况,我正在使用带有图像的水平滚动视图并使用按钮平滑滚动到不同的图像位置。现在它工作正常我只是想知道是否有人知道减慢平滑滚动方法,即有更长的动画时间?目前,捕捉发生得非常快。

也许通过覆盖平滑滚动,我试图搜索这个/示例但没有运气。

那么有什么想法吗?

谢谢,

4

7 回答 7

61

怎么样:

ObjectAnimator animator=ObjectAnimator.ofInt(yourHorizontalScrollView, "scrollX",targetXScroll );
animator.setDuration(800);
animator.start();
于 2013-07-22T12:37:24.387 回答
15

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

    new CountDownTimer(2000, 20) { 

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

        public void onFinish() { 

        } 
     }.start();

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

于 2011-03-04T12:45:12.520 回答
13

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

该调用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());
}
于 2012-11-30T04:39:22.373 回答
1

它是一个自动和连续滚动的滚动条。它是通过不断滚动图像列表来显示信用屏幕的。这可能会对您有所帮助或给您一些想法。

https://github.com/blessenm/SlideshowDemo

于 2012-03-11T08:06:48.383 回答
0

改为使用.smoothScrollToPositionFromTop。例子

listView.smoothScrollToPositionFromTop(scroll.pos(),0,scroll.delay());

哪里scroll是一个简单的变量,来自一个获取当前屏幕位置的类,.get()返回新的位置.pos()和平滑滚动的时间.delay......等等

于 2017-01-28T10:33:28.427 回答
0

或者更简单,.smoothScrollTo(). 例子:

hsv.smoothScrollTo(x, y);

文档:Android 开发者 ScrollView 文档

于 2021-01-13T00:47:10.033 回答
-5

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

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

于 2011-03-04T12:33:56.600 回答