Android: HorizontalScrollView smoothScroll animation time Android: HorizontalScrollView smoothScroll animation time android android

Android: HorizontalScrollView smoothScroll animation time


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


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