Android – canvas will not be updated (invalidated) until the end of the entire while loop
•
Android
I'm trying to move a ball on the canvas. A and B are similar to X and Y coordinate positions. Any way to try to get different values dynamically from my code. A and B are global variables. But it seems that "invalidate()" or screen refresh only occurs at the end of the whole cycle. Do you know why If I have to build it on another thread, please suggest me some simple code
private void shootBall(){
while (a>b){
a = getPositionX();
b = getPositionY();
invalidate();
}
}
}
resolvent:
Do this and use postinvalidate() instead:
private void shootBall(){
new Thread(new Runnable() {
public void run() {
while (a>b){
a = getPositionX();
b = getPositionY();
postInvalidate();
}
}
}).start();
}
Edit: but as mentioned earlier, don't assume that the screen is invalid. If it is marked for redrawing, the UI thread will bypass it
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码