Invalidate is not redrawing the screen. Android

前端 未结 2 1955
借酒劲吻你
借酒劲吻你 2021-01-22 07:33
BufferedReader hl  = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.lines)));
                while(hl.ready()){
                    showL         


        
2条回答
  •  囚心锁ツ
    2021-01-22 08:14

    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:

    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); 
         }
    

提交回复
热议问题