Erez Shmiel Erez Shmiel - 1 year ago 192
Android Question

Android: how to stop animation for a gif image button when pressed in Java?

I've this variable:

GifImageButton buttonGifImage = new GifImageButton(this);

I added it an animated gif:


When I load this code, I see the animation. I want to stop the animation when I press on this button. I tried this but it didn't work:

buttonGifImage.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_BUTTON_PRESS || event.getAction() == MotionEvent.ACTION_DOWN) {
// Here I would like to stop the animation
// I want to display the same image but without animating
buttonGifImage.setFreezesAnimation(true); //not working
buttonGifImage.clearAnimation(); //not working

Is there a good way to stop the animation? (considering I would like to activate the animation again).

Or must I create a png image from this gif image and call setBackgroundResource with this png..?

I'm trying to avoid the saving of 2 images(gif and png) for play\stop animated gif..

======== EDIT ========

my dependencies:

dependencies {
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile ''
compile ''

my repositories:

repositories {
maven { url "" }

Answer Source

use the getDrawable() method to get the GifDrawable object and call the stop() method:


or since you have setup as background resource:


use: ((GifDrawable)buttonGifImage.getBackground()).stop()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download