2

I've set global variables x,y in the Activity class.

I start a thread "t0" that continually update globals x and y.

I have onDraw pseudocode as follows (all on the UI thread):-

View.onDraw(){
    if (x,y changed value) {
        x0=x;
        y0=y;
        loop (x0-- until x0==0){
            canvas.drawBitmap(bmp, x0, y0, bitmapPaint);
            invalidate();
        }
    }
}

I was hoping that I'd see an animation of the bitmap moving across the screen on the x-axis, with each invalidate() re-drawing the new position. Instead I see it 'jump' to the last x position 0 (no intermediate stages).

I'm making the assumption that although x and y are updating via t0, I'm not too concerned since the loop is busy with the original x,y values (assigned to x0,y0).

I observe x,y updating and code is executed inside the 'if loop' (I see this via debug).

I tried adding a delay, but it didn't seem to make any difference. I can get it to re-draw directly to a new x,y position, but I need a smooth 'transition' via the loop to happen from one-x0-coord to another.

Any hints or tips would be appreciated.

Thanks in advance.

Steve

SteJav
  • 495
  • 4
  • 11

2 Answers2

0

I think you can't cause an onDraw within an onDraw. calling invalidate() only causes that onDraw will be called after the current onDraw finishes. I am not sure how you really implemented it since it's pseudocode but i imagine you don't want the loop in the onDraw but do somethinglike this:

View.onDraw(){
    if (x,y changed value) {
        x0=x;
        y0=y;
        if (x0!=0){
            x0--;
            canvas.drawBitmap(bmp, x0, y0, bitmapPaint);
            invalidate();
        }
    }
}
Ivo
  • 18,659
  • 2
  • 23
  • 35
0

What is happening here I guess is, intermediates are getting drawn. But there is not enough time to show those. you have to set some thread.sleep inside it. It will work.

AshMv
  • 392
  • 2
  • 12