简体   繁体   English

Android:Horizo​​ntalScrollView smoothScroll 动画时间

[英]Android: HorizontalScrollView smoothScroll animation time

I have a situation in that I am using a horizontal scroll view with images and using buttons to smooth scroll to the different image locations.我的情况是我使用带有图像的水平滚动视图并使用按钮平滑滚动到不同的图像位置。 Now it works okay I was just wondering if anyone knew of anyway to slow down the smooth scroll method, ie having a longer annimation time?现在它可以正常工作了,我只是想知道是否有人知道减慢平滑滚动方法的速度,即具有更长的动画时间? As currently the snapping happens pretty quickly.目前,捕捉发生得很快。

Perhaps through an override of the smoothscroll, I have tried to search for this/examples but to no luck.也许通过覆盖平滑滚动,我试图搜索这个/示例,但没有运气。

So any ideas?那么有什么想法吗?

Thanks,谢谢,

Si

How About:怎么样:

ObjectAnimator animator=ObjectAnimator.ofInt(yourHorizontalScrollView, "scrollX",targetXScroll );
animator.setDuration(800);
animator.start();

THis is one way, which works well for me:这是一种方法,对我来说效果很好:

    new CountDownTimer(2000, 20) { 

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

        public void onFinish() { 

        } 
     }.start();

So here the horizontal scroll view (hv) moves in two seconds from position 0 to 2000 or to the end of the view if smaller than 2000px.所以这里的水平滚动视图 (hv) 在两秒钟内从位置 0 移动到 2000 或如果小于 2000px 则移动到视图的末尾。 Easy to adjust...易于调整...

Subclass HorizontalScrollView, use reflection to get access to the private field mScroller in HorizontalScrollView.子类Horizo​​ntalScrollView,使用反射获取Horizo​​ntalScrollView中的私有字段mScroller Of course, this will break if the underlying class changes the field name, it defaults back to original scroll implemenation.当然,如果底层类更改字段名称,这将中断,它默认返回到原始滚动实现。

The call myScroller.startScroll(scrollX, getScrollY(), dx, 0, 500);调用myScroller.startScroll(scrollX, getScrollY(), dx, 0, 500); changes the scroll speed.改变滚动速度。

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());
}

Its a scroller the scroll automatically and continously.它是一个自动连续滚动的滚动条。 It was made to show a credits screen by continously scrolling through a list of images.它是通过连续滚动图像列表来显示信用屏幕的。 This might help you or give you some idea.这可能会帮助你或给你一些想法。

https://github.com/blessenm/SlideshowDemo https://github.com/blessenm/SlideshowDemo

Use .smoothScrollToPositionFromTop instead.改用.smoothScrollToPositionFromTop Example例子

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

where scroll is a simple variable from a class that takes current screen position .get() returns new position .pos() and time of smooth scrolling .delay ... etc其中scroll是一个来自类的简单变量,它采用当前屏幕位置.get()返回新位置.pos()和平滑滚动时间.delay ... 等

Or even easier, .smoothScrollTo() .或者更简单, .smoothScrollTo() Example:例子:

hsv.smoothScrollTo(x, y);

Docs: Android Developer ScrollView docs文档: Android 开发人员 ScrollView 文档

Have a look at http://developer.android.com/reference/android/widget/Scroller.html :看看http://developer.android.com/reference/android/widget/Scroller.html

The duration of the scroll can be passed in the constructor and specifies the maximum time that the scrolling animation should take滚动的持续时间可以在构造函数中传递,并指定滚动动画应该花费的最长时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM