Android: HorizontalScrollView smoothScroll animation time

醉酒当歌 提交于 2019-12-17 17:33:46

问题


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, i.e. 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


回答1:


How About:

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



回答2:


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. Easy to adjust...




回答3:


Subclass HorizontalScrollView, use reflection to get access to the private field mScroller in HorizontalScrollView. 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); 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());
}



回答4:


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




回答5:


Use .smoothScrollToPositionFromTop instead. Example

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

wherescroll is a simple variable from a class that takes current screen position .get() returns new position .pos() and time of smooth scrolling .delay ... etc




回答6:


Have a look at 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



来源:https://stackoverflow.com/questions/5193678/android-horizontalscrollview-smoothscroll-animation-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!