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

Mao*_*hen 4 java android animated-gif

我有这个变量:

GifImageButton buttonGifImage = new GifImageButton(this);
Run Code Online (Sandbox Code Playgroud)

我给它添加了一个动画 gif:

buttonGifImage.setBackgroundResource(R.drawable.gifImage);
Run Code Online (Sandbox Code Playgroud)

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

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么好办法让动画停止吗?(考虑到我想再次激活动画)。

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

我试图避免为播放\停止动画 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'
    }
Run Code Online (Sandbox Code Playgroud)

我的存储库:

    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
Run Code Online (Sandbox Code Playgroud)

ple*_*eft 7

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

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

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

buttonGifImage.setBackgroundResource(R.drawable.gifImage);

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