简体   繁体   中英

Android + How to add vertical “Auto-Scroll” feature to ScrollView or TextView

I'm making a document reader app and I want a user to be able to ask the app the auto-scroll a document instead of the manual scrolling. Please could anyone give me ideas on how to make a ScrollView "Auto-Scroll" vertically? Sort of like credits at the end of a movie. I set up a TimerTask to this below but my problems are:

  1. How do I get the increment value to feed into the TimerTask ie the new position the scrollbar should go to each time the TimerTask is run.

  2. Are there any better ways to achieve this auto-scroll feature?

My code is below, I achieved the auto-scroll but the scrolling wasn't even...(totally new to both Java and Android..smile)...thanks!!!

Timer scrollTimer = new Timer();
Handler handler = new Handler();
ScrollView scrollView = (ScrollView) findViewById(R.id.hse);
int scrollSpeed = 5; // this is set by the user in preferences

TimerTask scrollerSchedule = new TimerTask() {
        @Override
        public void run() {

            handler.post(new Runnable() {

                @Override
                public void run() {
                    //Here I want to increment the scrollbar's vertical  position by 10
        //scrollView.smoothScrollBy(0, scrollView.getScrollY() + 10);
                                     new moveScrollView().execute();
                }
            });
        }
    };

    scrollTimer.schedule(scrollerSchedule, 0, scrollSpeed * 100);

}

private class moveScrollView extends AsyncTask<Void, Void, Void> {

    protected void onProgressUpdate(Void... progress) {
        // show the user some sort of progress indicator
        // like how many minutes before scroll is completed

    }

    protected Void doInBackground(Void... params) {
        // TODO
        int scrollPos = (int) (scrollView.getScrollY() + 10.0);
        scrollView.smoothScrollTo(0, scrollPos);
        return null;
    }

}

Changes: Like @Kurtis suggested, I added the AsyncTask to carry out the scroll, and then used the TimerTask + Handler to do the repetition. Now when run the scroll code

int scrollPos = (int) (scrollView.getScrollY() + 10.0);
scrollView.smoothScrollTo(0, scrollPos);

in the AsynTask, I get an error, but when I run the code directly in my Runnable it works as expected. I'm still concerned about efficiency and would really want to use the AsynTask. Any tips on fixing my AsyncTask code?


BTW: This was my observation (for newbies like me). Previously I used

scrollView.smoothScrollBy(0, scrollView.getScrollY() + 10);

and the scroll start jumping unevenly, sort of incrementing the scroll value like +5, +10 +15 +.... instead of an even repetition like +5, +5, +5.... What I found out was that if I wanted to compute the incremental value like scrollView.getScrollY() + 10, then I should have used smoothScrollTo() instead of smoothScrollBy().

scrollView.smoothScrollTo(0, scrollView.getScrollY() + 10);

This is Slideshow demo I created to show the credits for one of my apps. Its source will give you an idea of how to proceed.

You can download the source and import the project to eclipse to try it out.

You should use a combination of AsyncTask to do the scrolling for you and the AlarmManager to do the repeating for you. At a high level, what you should do is create a PendingIntent that is a broadcast. In your activity create a broadcast reciever that recieves the type of broadcasts the PendingIntent will have. Every time that reciever recieves the broadcast is should launch an AsynTask whose doInBackground method is just:

scrollView.smoothScrollBy(0, scrollView.getScrollY() + 10);

Be careful though. Make sure to shut off the Alarm you create with the Alarm manager when ever your activity pauses (ie in the onPause method). If you don't you'll be firing off a bunch of broadcasts that never get use. This will drain the users battery. You should then also turn the Alarms back on in the onResume method.

Some other notes, make sure to use ELAPSED_REAL_TIME when setting your alarm in the alarm manager. Otherwise you're scrolling rate will vary with the speed of the processor on the device. You can adjust the time interval at which the broadcasts are sent out to make the view scroll faster or slower.

PS

For more advice on threading, checkout the Painless Threading article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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