View updatet sich nicht

  • Antworten:1
Assasin Knight
  • Forum-Beiträge: 3

18.02.2019, 22:29:37 via Website

Hi,

ich wollte eine eigene Animation in einem Thread erstellen und nach Beendigung soll es weiter gehen. Leider zeigt er nicht die Animation :'(. Weißt jemand wie ich das lösen kann?

mfg
Assasin :-X

MainActivity code:

TextView textbanner = (TextView) findViewById(R.id.meLbl);
 ChatAnimation chatAnimation = new ChatAnimation(textbanner);
    chatAnimation.start();


    //wait for animation
        try {
            chatAnimation.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

ChatAnimation code:

public class ChatAnimation extends Thread {

private TextView tv;
private int sleeptime = 100;

public ChatAnimation(TextView tv) {
    this.tv = tv;
}


public void run() {

    int i = 0;
    while (i < 4) {
        tv.post(new Runnable() {
            public void run() {
                tv.setText("EVA schreibt");
            }
        });
        try {
            sleep(sleeptime);
        } catch (InterruptedException e) {
        }
        tv.post(new Runnable() {
            public void run() {
                tv.setText("EVA schreibt .");
            }
        });
        try {
            sleep(sleeptime);
        } catch (InterruptedException e) {
        }
        tv.post(new Runnable() {
            public void run() {
                tv.setText("EVA schreibt ..");
            }
        });
        try {
            sleep(sleeptime);
        } catch (InterruptedException e) {
        }
        tv.post(new Runnable() {
            public void run() {
                tv.setText("EVA schreibt ...");
            }
        });
        try {
            sleep(sleeptime);
        } catch (InterruptedException e) {
        }

        i++;
    }
    tv.post(new Runnable() {
        public void run() {
            tv.setText("EVA");
        }
    });

}

}

Kommentieren
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

19.02.2019, 00:42:02 via App

Hallo Assasin,

UI Updates müssen in Android immer im MainThread gemacht werden, sonst funktioniert das nicht.

Evtl. solltest du hier mit einem Hander#postDelayed arbeiten.
Damit kannst du Code verzögert ausführen lassen.

Hier mal ein Beispiel (die Logik gleich vereinfacht) in Pseudocode

 TextView myTxt = findViewById(...);
 myTxt.setText("Eva schreibt");
 int count=0;
 Handler myHandler =new Handler();
 Runnable myRunnable =new ...
 run{

 myTxt.setText(myTxt.getText()+".");//Ein Punkt anhängen
 count++
 if(count <4) myHandler.postDelayed(this,500);//Restart

 };

 handler.postDelayed(myRunnable,500);//Start nach 500ms

LG Pascal //It's not a bug, it's a feature. :) ;)

Hilfreich?
Kommentieren