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

4

I've this variable:

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..?

我试图避免为播放\停止动画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" }
    }

你使用哪个库来实现GifImageButton? - Cüneyt
我在问题中添加了“编辑”。如果它没有回答你的问题,你能告诉我如何检查吗? - Maor Cohen
1个回答

8
使用getDrawable()方法获取GifDrawable对象并调用stop()方法: ((GifDrawable)buttonGifImage.getDrawable()).stop() 或者,由于您已将其设置为背景资源: buttonGifImage.setBackgroundResource(R.drawable.gifImage); 使用:((GifDrawable)buttonGifImage.getBackground()).stop()

1
尝试过,但由于“null”而崩溃。stop(); 继承:GifImageButton:ImageButton:ImageView:View - Maor Cohen
1
getDrawable()更改为getBackground() - pleft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接