Hassam Bin Hassan
Hassam Bin Hassan

Reputation: 419

How to set animation on Bitmap in Canvas onDraw(), Android?

Can anyone help me how to set animation on two bitmaps in Canvas? I have two bitmaps "bitmap1" and "bitmap2".

I want to display "bitmap1" and then "bitmap2" with interval of 500ms then again bitmap1 and so on this continues... I want any method to do this but not using Thread.sleep(500);

Thanks in advance...

Upvotes: 2

Views: 2421

Answers (1)

Samuel Rabinowitz
Samuel Rabinowitz

Reputation: 135

Try something like this in the thread:

long lastBitmapSwitchMillis = System.currentTimeMillis();   //Saves system time from last bitmap switch
int currentBitmap = 1;   //1 = bitmap1, 2 = bitmap2
int bitmapInterval = 500;   //Interval between bitmap switches

while (running) {

     //Switches bitmap after interval
     if (System.currentTimeMillis() >= lastBitmapSwitchMillis + bitmapInterval) {
          lastBitmapSwitchMillis = System.currentTimeMillis();   //Save current time of bitmap switch
          if (currentBitmap == 1) {
               currentBitmap = 2;
          }
          else if (currentBitmap == 2) {
               currentBitmap = 1;
          }
     }

     //Render appropriate bitmap
     if (currentBitmap = 1) {
          canvas.drawBitmap(bitmap1, x, y, paint);   //x and y are bitmap's location,
     }
     else if (currentBitmap = 2) {
          canvas.drawBitmap(bitmap2, x, y, paint);   //x and y are bitmap's location
     }
}

Upvotes: 1

Related Questions