【问题标题】:Android: ObjectAnimator with long durationAndroid:持续时间长的 ObjectAnimator
【发布时间】:2015-08-18 21:34:43
【问题描述】:

我正在尝试使用 ObjectAnimator 制作搜索栏动画。问题是如果我将 ObjectiAnimator 的持续时间设置为较大的值,它会在开始之前有延迟。这是我的代码:

_replayBar.setMax(FlightLogger.getTimeLength());
_replayBar.setOnSeekBarChangeListener(this);
_timeMax.setText(secondsToTimeFormat(0) + "/" + secondsToTimeFormat(_replayBar.getMax()));

anim=ObjectAnimator.ofInt(_replayBar, "progress", 0, _replayBar.getMax());
anim.setDuration(_replayBar.getMax() * 1000);

其中 anim 是 objectAnimator,_replaybar 是 seekbar,FlightLogger.getTimeLenth() 以毫秒为单位返回时间。

我要做的只是让搜索栏实时更新过程以秒为单位。但是如果传递给 anim.setDuration 的值变得太大(即:2分钟),它将延迟启动几秒钟。如果持续时间更长(即:1 小时),延迟可能是几分钟。

我也尝试使用 ValueAnimator,代码类似,加上 onAnimationUpdate 监听器如下:

anim.addUpdateListener(new ObjectAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        if (!anim.isRunning())
            anim.resume();
        animProgress = (Integer) valueAnimator.getAnimatedValue();
        Log.d(TAG, "progress: " + valueAnimator.getAnimatedValue());
        _replayBar.setProgress(animProgress);
    }
});

我在侦听器中记录进程,并为每个进程返回重复值,类似于ValueAnimator duplicate Values when starting

谁能帮我解决这个问题? 非常感谢

【问题讨论】:

  • 您希望多久更改一次“progress”属性?
  • 每秒,类似于播放视频
  • 所以使用android.os.CountDownTimer或原始Handler,动画师不适合这种用例
  • 我试试吧。感谢您的帮助

标签: android objectanimator


【解决方案1】:

解决了我的 CountDownTimer 问题,感谢 pskink 的建议。 这是计时器类:

public class BCountDownTimer extends CountDownTimer{

    public BCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long l) {
        millisLeft=l;
        int seekBarProgress= _replayBar.getMax()-(int)(l*0.001);
        _replayBar.setProgress(seekBarProgress);
    }

    @Override
    public void onFinish() {
        _replayBar.setProgress(_replayBar.getMax());
        isPlaying = false;
        _playPause.setImageResource(R.drawable.ic_btn_play);
        _timer= new BCountDownTimer(_replayBar.getMax()*1000, 1000);
    }
}

为了暂停和恢复,millisLeft 存储剩余时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 2014-12-17
    • 2016-03-01
    • 2020-04-10
    • 2020-07-03
    • 1970-01-01
    • 2012-03-14
    相关资源
    最近更新 更多