ScrollView autoscrolling

2019-09-21 09:19发布

First of all let me excuse for this question, I know that many other people asked it here, but I am so stupid that did not find any correct answer for me. I have a ScrollView and many TextViews and ImageViews inside of it. I want to scroll all this stuff across the screen like a movie's credits. I found this method. It works but only for a few seconds and then stops.:

            public void scrollRight(final ScrollView h){
    new CountDownTimer(2000, 1) { 

        public void onTick(long millisUntilFinished) { 
            h.scrollTo(0,(int) (2000-millisUntilFinished)/25);

        } 

        public void onFinish() { 

        } 
     }.start(); }

2条回答
女痞
2楼-- · 2019-09-21 09:29

You can also use ObjectAnimator to achieve this:

ObjectAnimator.ofInt(view, "scrollY", 0, view.getBottom).setDuration(1000).start;

查看更多
Juvenile、少年°
3楼-- · 2019-09-21 09:34

you can use a scroller to create a smooth animation of the scroll. there's an example here: Android: Scroller Animation?

basically, all you need to do is start the scroller from 0 to the end of the list and call the scrollTo of the list with the scroller value.

another option is to create a timer that calls scrollBy every time.

EDIT: also, the way you created CountDownTimer causes it to run for 2000 milliseconds, and call onTick every 1ms. If you want it to run longer, just increase the overall time. I also suggest using a bigger interval - 1ms is way too often for display purposes. Try this:

new CountDownTimer(10000, 25) { 
     public void onTick(long millisUntilFinished) { 
     h.scrollBy(1,0);
}
查看更多
登录 后发表回答