2012-06-02 73 views
6

我是新來的android。我正在嘗試對橫向搜索欄進行動畫製作,但目前還沒有做到。 我只想要一個動畫,其中seekbar在一段時間內顯示進度,例如1分鐘。 有人可以建議/給出想法/代碼片段我應該如何動畫標準seekbar?如何使用動畫動畫seekbar

我應該使用什麼樣的動畫像objectanimator或valueAnimation? 我是否需要定義一個運行方法(不確定!)來將拇指動畫到下一個位置?

在此先感謝。

回答

13

這樣做的一個方法是使用ValueAnimator:

final SeekBar seekBar = findViewById(R.id.seekBar); 
ValueAnimator anim = ValueAnimator.ofInt(0,seekBar.getMax()); 
anim.setDuration(1000); 
anim.addUpdateListener(new AnimatorUpdateListener() { 

     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      int animProgress = (Integer) animation.getAnimatedValue(); 
      seekBar.setProgress(animProgress); 
     } 
    }); 

另一種方式可能是(還沒有測試這一點):

final SeekBar seekBar = findViewById(R.id.seekBar); 
ObjectAnimator anim = ObjectAnimator.ofFloat(seekBar, "progress", 0,seekBar.getMax()); 
anim.setDuration(10000); 
anim.start(); 
+0

注意,這兩個建議的方法需要蜂窩(API 11,又名3.0)或更高版本。 –

+5

當嘗試使用第二個選項時,Seekbar在查看日誌的視圖上沒有響應,我得到了警告錯誤_「方法setProgress()和目標類android.widget.seekbar上未找到類型float」。_因爲設置當前語法ObjectAnimator的方式期望SeekBar實例有一個'setProgress(float val)'方法,但我猜SeekBar只有一個'setProgress(int val)'方法。使用ObjectAnimator.ofInt'而不是ObjectAnimator.ofFloat'來處理SeekBars –

+0

@MarquisBlount man,謝謝,我得到了問題:「setProgress()方法在目標類X上找不到類型float」。和你的評論是有幫助的:) –