Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

buttonGifImage.setBackgroundResource(R.drawable.gifImage);

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() {
    @Override
    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 'com.android.support:appcompat-v7:23.2.1'
        compile 'com.android.support:design:23.2.1'
    }

my repositories:

    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
like image 821
Maor Cohen Avatar asked Mar 11 '23 17:03

Maor Cohen


1 Answers

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

((GifDrawable)buttonGifImage.getDrawable()).stop()

or since you have setup as background resource:

buttonGifImage.setBackgroundResource(R.drawable.gifImage);

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

like image 144
pleft Avatar answered Apr 26 '23 16:04

pleft