Android: How to stop animation of gif image button when pressed in Java?
I have this variable :
GifImageButton buttonGifImage = new GifImageButton(this);
I added a gif animation to it:
buttonGifImage.setBackgroundResource(R.drawable.gifImage);
When I load this code, I see the animation. I want to stop the animation when I press this button. I tried 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 any good way to stop the animation? (Considering I want to stimulate activity painting again).
Or do I have to create a png image from this gif image and call setBackgroundResource..with this png?
I’m trying to avoid saving 2 images (gif and png) for play\stop animated gifs…
======
==== 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 repository:
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
Solution
Use the getDrawable() method
to get the GifDrawable
object and call the stop()
method:
((GifDrawable)buttonGifImage.getDrawable()).stop()
Or because you’ve set up a background resource:
buttonGifImage.setBackgroundResource(R.drawable.gifImage);
Use: ((GifDrawable)buttonGifImage.getBackground()).stop().