Как установить анимацию на индикатор выполнения в Android - PullRequest
0 голосов
/ 30 октября 2018

Я хочу создать индикатор выполнения таймера на 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 голосов
/ 01 августа 2019

В 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)
0 голосов
/ 30 октября 2018

вы можете использовать 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();
0 голосов
/ 30 октября 2018

Используйте следующий код, чтобы получить ваш прогресс

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

valueAnimator.getAnimatedFraction() дает значение дроби вашей анимации. Вам нужно умножить на максимальный прогресс / длительность, чтобы получить текущее значение прогресса.

0 голосов
/ 30 октября 2018

сделать класс

 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);  
...