animationDrawable仅显示最后一帧

时间:2019-03-02 10:39:39

标签: java animationdrawable

我正在尝试从多个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是字符串的向量。每个字符串都是以编码的图像本身。这就是为什么client._fromServer.elementsAt(i)是需要解码并转换为byteArray的字符串的原因。

1 个答案:

答案 0 :(得分:0)

我认为这是因为您从同一Drawable获得了ImageView
当您执行image.setImageBitmap()时,它将更新ImageView中Drawable的引用,并且AnimationDrawable也将受到影响。
您应该为每个Drawable调用使用不同的addFrame实例。

类似的东西:

AnimationDrawable animation = new AnimationDrawable();
ImageView image = (ImageView) findViewById(R.id.sign);

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);
    final Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false);
    Drawable drawable = new BitmapDrawable(getResources(), scaledBitmap);
    animation.addFrame(drawable, 1000);
}

animation.setOneShot(true);
animation.start();