2016-11-08 82 views
1

我有這個變量:Android:在Java中按下時如何停止GIF圖像按鈕的動畫?

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

我試圖避免2幅圖像(GIF和PNG)爲發揮節約\停止gif動畫..

========編輯======= =

我的依賴關係:

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" } 
    } 
+0

您使用GifImageButton的哪個庫? –

+0

我加了編輯的問題。如果它沒有回答你的問題,你能告訴我如何檢查它嗎? –

回答

1

使用getDrawable()方法來獲得GifDrawable對象,並調用stop()方法:

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

或者因爲你必須設置爲背景資源:

buttonGifImage.setBackgroundResource(R.drawable.gifImage);

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

+0

嘗試過,因爲:「null」.stop(); 繼承: GifImageButton:ImageButton:ImageView:View –

+1

將'getDrawable()'改爲'getBackground()' – pleft

+0

太棒了,它的工作原理! –