這樣做的一個方法是使用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();
注意,這兩個建議的方法需要蜂窩(API 11,又名3.0)或更高版本。 –
當嘗試使用第二個選項時,Seekbar在查看日誌的視圖上沒有響應,我得到了警告錯誤_「方法setProgress()和目標類android.widget.seekbar上未找到類型float」。_因爲設置當前語法ObjectAnimator的方式期望SeekBar實例有一個'setProgress(float val)'方法,但我猜SeekBar只有一個'setProgress(int val)'方法。使用ObjectAnimator.ofInt'而不是ObjectAnimator.ofFloat'來處理SeekBars –
@MarquisBlount man,謝謝,我得到了問題:「setProgress()方法在目標類X上找不到類型float」。和你的評論是有幫助的:) –