Android: 如何在按下 Java 时停止 gif 图像按钮的动画?

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

我有这个变量:

GifImageButton buttonGifImage = new GifImageButton(this);

我给它添加了 gif 动画:

buttonGifImage.setBackgroundResource(R.drawable.gifImage);

当我加载这段代码时,我看到了动画。我想在按下此按钮时停止动画。我试过了,但没用:

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
        }
    }
}

有什么好的方法可以停止动画吗?(考虑到我想再次激活动画)

或者我必须从这个 gif 图像创建一个 png 图像并用这个 png 调用 setBackgroundResource..?

我试图避免为 play\stop 动画 gif 保存 2 张图像(gif 和 png)..

========编辑========

我的依赖项:

    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'
    }

我的存储库:

    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }

使用getDrawable()方法获取GifDrawable对象并调用stop()方法:

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

或者由于您已设置为后台资源:

buttonGifImage.setBackgroundResource(R.drawable.gifImage);

使用:((GifDrawable)buttonGifImage.getBackground()).stop()