Home > front end >  Display GIF as button compound drawable programmatically
Display GIF as button compound drawable programmatically

Time:01-08

For the past few days, I am trying to set the icon of one of my Buttons as a GIF file programmatically. My GIF is located in my assets.

This is the code that I tried. It partially works, but the GIF seems to be stopped and is not playing, like just an ordinary PNG file.

AssetManager assetManager = context.getAssets();
Drawable drawable = null;
    try {
        drawable = Drawable.createFromStream(context.getAssets().open("test.gif"), null);
        button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0)
    } catch (IOException e) {
        e.printStackTrace();
    }

I am expecting the drawable to be playing, just like a GIF.

Any help is appreciated. Thank you.

CodePudding user response:

AssetManager assetManager = context.getAssets();

GifDrawable drawable = null;
try {
    drawable = new GifDrawable(context.getAssets(), path);
    button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} catch (IOException e) {
    e.printStackTrace();
}

Hey, hope this helps you with your question, you could add this to your .gradle file.

  • Related