无效不重绘屏幕。安卓不重、屏幕

2023-09-07 16:48:24 作者:説冭多卟筎沉黙

BufferedReader hl  = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.lines)));
                while(hl.ready()){
                    showLines.append(hl.readLine()+"\n");
                    showLines.invalidate();
                    Thread.sleep(10);
                }

这是我的code,但是当我告诉它这是不是重绘。据推测每个被加到TextView的行之后重新绘制,但它仍然只重画在结束了吗?是否有人可以帮助我,我也搞不清楚。

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.

推荐答案

这是bacause您无效()是在一个线程中while循环,并正在acummulated,可以这么说,直到循环结束,并且只比它吸引。 ..

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...

我在一个循环中使用Thread.sleep()方法时,有同样的问题。可以使用一个后延迟方法绘制的每一行,在此情况下是每秒1行:

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:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    BufferedReader hl  = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.text))); 
    TextView showLines = (TextView) findViewById(R.id.textView1);
    appendLines(hl, showLines);

}


public void appendLines(final BufferedReader br, final TextView tv){
    Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() {           
         public void run() {                
             try {
                if(br.ready()){                     
                     try {
                        tv.append(br.readLine()+"\n");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }                     
                     tv.invalidate();                     
                     appendLines(br, tv);             
                     }
            } catch (IOException e) {
                e.printStackTrace();
            } 
         }
         }, 1000); 
     }