简体   繁体   English

Android +如何向ScrollView或TextView添加垂直“自动滚动”功能

[英]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? 请问有人可以提出如何垂直制作ScrollView“Auto-Scroll”的想法吗? Sort of like credits at the end of a movie. 在电影结束时有类似的信用。 I set up a TimerTask to this below but my problems are: 我在下面设置了一个TimerTask但我的问题是:

  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. 如何将增量值输入TimerTask,即每次运行TimerTask时滚动条应该进入的新位置。

  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!!! 我的代码在下面,我实现了自动滚动,但滚动甚至没有......(Java和Android..smile都是全新的)...谢谢!

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. 更改:像@Kurtis建议的那样,我添加了AsyncTask来执行滚动,然后使用TimerTask + Handler进行重复。 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. 在AsynTask中,我收到一个错误,但是当我直接在我的Runnable中运行代码时,它按预期工作。 I'm still concerned about efficiency and would really want to use the AsynTask. 我仍然关注效率,并且真的想要使用AsynTask。 Any tips on fixing my AsyncTask code? 有关修复AsyncTask代码的任何提示?


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(). 滚动开始跳得不均匀,有点像滚动值增加+5,+ 10 + 15 + ....而不是像+ 5,+ 5,+ 5那样的偶数重复....我发现的是那个如果我想像scrollView.getScrollY()+ 10那样计算增量值,那么我应该使用smoothScrollTo()而不是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. 您可以下载源代码并将项目导入eclipse进行试用。

You should use a combination of AsyncTask to do the scrolling for you and the AlarmManager to do the repeating for you. 您应该使用AsyncTask的组合为您进行滚动,并使用AlarmManager为您重复。 At a high level, what you should do is create a PendingIntent that is a broadcast. 在高级别,您应该做的是创建一个广播的PendingIntent。 In your activity create a broadcast reciever that recieves the type of broadcasts the PendingIntent will have. 在您的活动中,创建一个广播接收器,接收PendingIntent将具有的广播类型。 Every time that reciever recieves the broadcast is should launch an AsynTask whose doInBackground method is just: 每当接收者收到广播时,都应该启动一个AsInTask,其doInBackground方法就是:

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). 确保在活动暂停时关闭使用警报管理器创建的警报(即在onPause方法中)。 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. 然后,您还应该在onResume方法中重新打开警报。

Some other notes, make sure to use ELAPSED_REAL_TIME when setting your alarm in the alarm manager. 在警报管理器中设置警报时,请务必使用ELAPSED_REAL_TIME。 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 PS

For more advice on threading, checkout the Painless Threading article. 有关线程的更多建议,请查看无痛线程文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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