【发布时间】:2019-03-02 10:39:39
【问题描述】:
我正在尝试使用多个 png 图像创建动画。这是我的代码:
AnimationDrawable animation = new AnimationDrawable();
for (int i = 0; i < translate_text.length(); i++)
{
byte[] byteArray = Base64.getDecoder().decode(client._fromServer.elementAt(i));
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.sign);
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
animation.addFrame(image.getDrawable(), 1000);
}
animation.setOneShot(true);
animation.start();
但这只显示最后一帧...有什么想法吗?
编辑:可能应该早点这样做,但这里是:
translate_text 是一个字符串。它代表图像序列。例如,如果字符串是“bob”,那么应该有 3 个图像:字母 B、字母 O 和字母 B。
client._fromServer 是一个字符串向量。每个字符串都是以 base64 编码的图像本身。这就是为什么 client._fromServer.elementsAt(i) 是一个需要解码并转换为 byteArray 的字符串。
【问题讨论】:
-
调试代码时,
translate_text的值是多少?您确定每次循环迭代都会得到不同的图像吗?这是什么client._fromServer.elementAt(i)(真的很奇怪)? -
translate_text 是一个字符串,其中每个字符代表一个图像(例如,如果字符串是“bob”,那么我将有 3 个图像:字母 B、字母 O 和字母 B)。 client._fromServer.elementAt(i) 是图像的字符串表示形式(以 base64 编码)。是的,我真的很确定它在每次循环迭代时都会得到不同的图像@florian