谁能帮我在Canvas中的两个位图上设置动画?
我有两个位图"bitmap1"
和"bitmap2"
。
我想显示"bitmap1"
,然后显示"bitmap2"
,间隔为500ms
,然后再次显示bitmap1
,依此类推,继续...
我想要任何方法执行此操作,但不使用Thread.sleep(500);
提前致谢...
最佳答案
在线程中尝试以下操作:
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
}
}