我们可以利用 Imageview setImageResource 因为这比 drawable 看起来更有效,请参阅下面的代码。
如果您有 gif 的多个拆分图像,则以下代码可用于显示类似 gif 的图像。只需从在线工具将 gif 拆分为单独的 png 并将图像放入 drawable 中,如下所示
image_1.png、image_2.png 等
让处理程序动态更改图像。
int imagePosition = 1;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
updateImage();
}
};
public void updateImage() {
appInstance.runOnUiThread(new Runnable() {
@Override
public void run() {
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
gifImageViewDummy.setImageResource(resId);
imagePosition++;
//Consider you have 30 image for the anim
if (imagePosition == 30) {
//this make animation play only once
handler.removeCallbacks(runnable);
} else {
//You can define your own time based on the animation
handler.postDelayed(runnable, 50);
}
//to make animation to continue use below code and remove above if else
// if (imagePosition == 30)
//imagePosition = 1;
// handler.postDelayed(runnable, 50);
//
}
});
}