我是新手,对Android不太熟悉。我想尝试为水平SeekBar添加动画效果,但一直没有成功。
我只想要一个动画效果,在一定时间内(比如1分钟),SeekBar就能显示进度。
有人可以给出建议/想法/代码片段吗?应该使用哪种动画,比如ObjectAnimator或ValueAnimator?还需要定义一个run方法来将滑块动画到下一个位置吗?(不确定!)
提前感谢您的帮助。
其中一种方法是使用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();
ObjectAnimator
期望SeekBar实例具有setProgress(float val)
方法,但我猜SeekBar只有一个setProgress(int val)
方法。要使其工作,请在处理SeekBar时使用ObjectAnimator.ofInt
而不是ObjectAnimator.ofFloat
。 - Marquis Blount