Reduce speed of smooth scroll in scroll view

后端 未结 5 1003
栀梦
栀梦 2021-02-03 19:44

I have a scroll View. I performed smooth-scroll.using smoothScrollBy().It all works fine, but I want to change the duration of the smooth-scroll. Smooth-scroll happens very fast

相关标签:
5条回答
  • 2021-02-03 19:57

    The simple answer is just to replace

    scrollView.smoothScrollTo(0, scrollTo);
    

    with

    ObjectAnimator.ofInt(scrollView, "scrollY",  scrollTo).setDuration(duration).start();
    

    where duration is the time in milliseconds you want it to take.

    0 讨论(0)
  • 2021-02-03 20:04

    Here is a solution with a timer which uses scrollTo but you can also use scrollBy Android: HorizontalScrollView smoothScroll animation time

    0 讨论(0)
  • 2021-02-03 20:17
    whichScreen = Math.max(0, Math.min(whichScreen, getBase().getDocumentModel().getPageCount()-1));
    mNextScreen = whichScreen;
    final int newX = whichScreen * getWidth();
    final int delta = newX - getScrollX();
    //scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
    scroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta));
    invalidate();
    
    0 讨论(0)
  • 2021-02-03 20:19

    Try the following code:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        ValueAnimator realSmoothScrollAnimation = 
            ValueAnimator.ofInt(parentScrollView.getScrollY(), targetScrollY);
        realSmoothScrollAnimation.setDuration(500);
        realSmoothScrollAnimation.addUpdateListener(new AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                int scrollTo = (Integer) animation.getAnimatedValue();
                parentScrollView.scrollTo(0, scrollTo);
            }
        });
    
        realSmoothScrollAnimation.start();
    }
    else
    {
        parentScrollView.smoothScrollTo(0, targetScrollY);
    }
    
    0 讨论(0)
  • 2021-02-03 20:20

    This is how I achieved a smooth vertical scroll (like movie credits). This also allows the user to move the scroll up and down and allow it to continue scrolling when they let go. In my XML, I encapsulated my TextView inside of a ScrollView called "scrollView1". Enjoy!

        final TextView tv=(TextView)findViewById(R.id.lyrics);
        final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
        Button start = (Button) findViewById(R.id.button_start);
        Button stop = (Button) findViewById(R.id.button_stop);
        final Handler timerHandler = new Handler();
        final Runnable timerRunnable = new Runnable() {
            @Override
            public void run() {
                scrollView.smoothScrollBy(0,5);         // 5 is how many pixels you want it to scroll vertically by
                timerHandler.postDelayed(this, 10);     // 10 is how many milliseconds you want this thread to run
            }
        };
    
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               timerHandler.postDelayed(timerRunnable, 0);
            }
        });
    
        stop.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                timerHandler.removeCallbacks(timerRunnable);
            }
        });
    
    0 讨论(0)
提交回复
热议问题