如何在Android中将动画设置为进度条

时间:2018-10-30 09:41:10

标签: android android-animation android-progressbar

我想创建1分钟的计时器进度栏。我使用了以下代码,但是它是从60到0开始的,而使用相同的过程我必须是从0到60开始。

public static void animateTimerProgress(int currentTimeDuration, final int maxTimeDuration, boolean withStartDelay, final DonutProgress timeOutProgressView, Runnable endAction) {
    final int duration = currentTimeDuration < 0 ? 1 : currentTimeDuration;
    timeOutProgressView.setMax(maxTimeDuration);
    timeOutProgressView.setProgress(duration);
    timeOutProgressView.animate()
            .setDuration(duration * SECOND_IN_MILLIS)
            .setInterpolator(new LinearInterpolator())
            .setStartDelay(withStartDelay ? TIMEOUT_START_DELAY : 0)
            .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int progress = 1 + (int) ((1f - valueAnimator.getAnimatedFraction()) * (duration - 1f));

                    timeOutProgressView.setProgress(progress);
                }
            })
            .withEndAction(endAction)
            .start();
}

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

您可以像这样使用ValueAnimator

ValueAnimator animator = ValueAnimator.ofInt(0, 60);
animator.setDuration(duration * SECOND_IN_MILLIS);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    public void onAnimationUpdate(ValueAnimator animation) {
        timeOutProgressView.setProgress((int) animation.getAnimatedValue());
    }
});
animator.start();

答案 1 :(得分:1)

使用以下代码获取进度

int progress = 1 + (int) (valueAnimator.getAnimatedFraction() * (duration - 1f));

valueAnimator.getAnimatedFraction()给出动画的分数值。您需要乘以最大进度/持续时间,以获得当前的进度值。

答案 2 :(得分:0)

上课

 public class Anim {

    public static void fadeOutProgressLayout(Activity act, final ViewGroup progressLayout) {
        act.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Animation fadeOut = new AlphaAnimation(1, 0);
                fadeOut.setInterpolator(new AccelerateInterpolator());
                fadeOut.setDuration(300);

                fadeOut.setAnimationListener(new Animation.AnimationListener()
                {
                    public void onAnimationEnd(Animation animation)
                    {
                        progressLayout.setVisibility(View.GONE);
                    }
                    public void onAnimationRepeat(Animation animation) {}
                    public void onAnimationStart(Animation animation) {}
                });

                progressLayout.startAnimation(fadeOut);
            }
        });
    }
}

将此行写到您希望进度进行的任何地方

Anim.fadeOutProgressLayout(GamblingVideosActivity.this, progressLayout);  

答案 3 :(得分:0)

在Kotlin上,您可以使用2个扩展功能来帮助解决此问题,并在执行过程中以1递增1。这样,您可以制作出更流畅的动画:

/**
 * ProgressBar Extensions
 */
fun ProgressBar.setBigMax(max: Int) {
    this.max = max * 1000
}

fun ProgressBar.animateTo(progressTo: Int, startDelay: Long) {
    val animation = ObjectAnimator.ofInt(
        this,
        "progress",
        this.progress,
        progressTo * 1000
    )
    animation.duration = 500
    animation.interpolator = DecelerateInterpolator()
    animation.startDelay = startDelay
    animation.start()
}

如何使用它:

 progress.setBigMax(10)
 progress.animateTo(10, 100)