如何使用动画来动画搜索栏

Pal*_*i g 6 animation android seekbar

我是android的新手.我试图为水平搜索栏设置动画但是无法做到这一点.我只想要一个动画,其中搜索栏在一段时间内显示进度,比如1分钟.有人可以建议/提供想法/代码片段,我该如何设置标准搜索栏的动画?

我应该使用像objectanimator或valueAnimation这样的动画?我是否需要定义一个运行方法(不确定!)来为拇指设置动画以进入下一个位置?

提前致谢.

Zel*_*ion 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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

另一种方式可能是(没有测试这个):

final SeekBar seekBar =  findViewById(R.id.seekBar); 
ObjectAnimator anim = ObjectAnimator.ofFloat(seekBar, "progress", 0,seekBar.getMax());
anim.setDuration(10000);
anim.start();
Run Code Online (Sandbox Code Playgroud)

  • 当试图使用第二个选项时Seekbar在查看日志的视图上没有响应时,我收到了警告错误_"方法setProgress(),在目标类android.widget.seekbar上找不到类型为float."_这是因为当前语法设置的方式`ObjectAnimator`期望SeekBar实例有一个`setProgress(float val)`方法,但我猜SeekBar只有一个`setProgress(int val)`方法.在处理SeekBars时,使用`ObjectAnimator.ofInt`代替`ObjectAnimator.ofFloat` (5认同)