I'm trying to create an animation out of multiple png images. Here's my code:
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();
but this only displays the last frame... Any ideas?
Edit: Probably should've done this earlier, but here goes:
translate_text is a string. It represents the image sequence. For example if the string is "bob" then there should be 3 images: the letter B, the letter O and the letter B.
client._fromServer is a vector of strings. Each string is the image itself encoded in base64. That's why client._fromServer.elementsAt(i) is a string that needs to be decoded and turned into byteArray.
I think it is because you get the Drawable
from the same ImageView
.
When you do image.setImageBitmap()
it updates the reference of the Drawable in the ImageView and the AnimationDrawable gets affected also.
You should use a different Drawable
instance for each addFrame
call.
Something like that:
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();