如何在Canvas onDraw(),Android上的Bitmap上设置动画?

时间:2014-07-21 15:54:20

标签: android animation android-canvas android-animation bitmapfactory

任何人都可以帮我解决如何在Canvas中的两个位图上设置动画的问题吗? 我有两个位图"bitmap1""bitmap2"

我希望"bitmap1"再显示"bitmap2",间隔为500ms,然后再显示bitmap1,依此类推... 我想要任何方法来执行此操作但不使用Thread.sleep(500);

提前致谢...

1 个答案:

答案 0 :(得分:1)

在主题中尝试这样的事情:

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
     }
}