The canvas is not updated (invalid) until the whole cycle ends

I'm trying to move the ball to the canvas. a and b are similar to the x, y coordinates. In any case, from my code I am trying to get different values ​​dynamically. A, b are global variables. But it seems that the "invalidate ()" or screen update occurs only after the end of the full cycle. You know why?. And if I need to build this in another thread, please offer me some simple codes.

private void shootBall(){
    while (a>b){
        a = getPositionX();
        b = getPositionY();
        invalidate();
        }
    }
}
+3
source share
4 answers

do it like 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: , , , invalidate , , , .

+1

, , invalidate() , ( ) . , invalidate ( ) , onDraw(). , / , . , invalidate() ... , .

, - (, , ). , invalidate() . , .

+11
0

The user interface cannot be changed from any new thread. You should use invalidate () in the same thread as your view

0
source

All Articles