无效并不是重画屏幕。安卓
BufferedReader hl = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.lines)));
while(hl.ready()){
showLines.append(hl.readLine()+"\n");
showLines.invalidate();
Thread.sleep(10);
}
这是我的代码,但当我告诉它时它不会重绘。它应该在添加到 textview 的每一行之后重绘,但它仍然只在最后重绘?有人可以帮我吗,我想不通。
BufferedReader hl = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.lines)));
while(hl.ready()){
showLines.append(hl.readLine()+"\n");
showLines.invalidate();
Thread.sleep(10);
}
That is my code but it is not redrawing when I tell it to. It is supposed to redraw after every line that is added to textview, but it still only redraws at the end? Can someone please help me, I can't figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为你的 invalidate() 位于一个线程 while 循环中,并且正在累积,可以这么说,直到循环结束,并且只有它绘制...
我在循环中使用 Thread.sleep() 时遇到了同样的问题。您可以使用后延迟方法来绘制每条线,在本例中为每秒一条线:
That is bacause your invalidate() is in a thread while loop and is being acummulated, so to speak, until the loop ends, and only than it draws...
I had the same problem when using Thread.sleep() within a loop. You can use a post delayed method to draw each line, which in this case is one line per second:
invalidate()
在您调用它时不会立即发生。invalidate()
与append()
以及任何涉及 UI 的其他内容一样,将一条消息放入消息队列中,一旦您执行此操作,主应用程序线程就会处理该消息让它吧。由于您将用户的时间浪费在无意义的sleep()
调用上,再加上在主应用程序线程的循环中执行 flash I/O,因此主应用程序线程无法处理消息队列上的消息。循环结束后,它将处理所有invalidate()
和append()
调用,并且您从所在的任何回调将控制权返回给 Android。不,不是。
正确的。
简单的解决方案是摆脱
invalidate()
和Thread.sleep(10)
并将整个文件内容加载到TextView< /code> 在一次调用中。
更好的解决方案是通过
AsyncTask
读取整个文件,然后在onPostExecute()
的一次调用中将文本附加到TextView
>。如果需要,请使用ProgressDialog
或其他东西来让用户在此过程中保持娱乐。invalidate()
does not happen immediately when you call it.invalidate()
, likeappend()
and anything else involving the UI, puts a message on a message queue, that will be processed by the main application thread as soon as you let it. Since you are wasting the user's time in pointlesssleep()
calls, plus doing flash I/O, in a loop on the main application thread, the main application thread cannot process the messages on the message queue. It will process all of yourinvalidate()
andappend()
calls after your loop is over and you return control to Android from whatever callback you are in.No, it isn't.
Correct.
The simple solution is for you to get rid of the
invalidate()
and theThread.sleep(10)
and just load the entire file contents into yourTextView
in one call.The better solution is for you to read the whole file in via an
AsyncTask
, then append the text to theTextView
in one call inonPostExecute()
. If needed, use aProgressDialog
or something to keep the user entertained while this is going on.