如何在 android 中为 progressBar 设置流畅的动画

How to set smoothly animation to progressBar in android

在我的项目中我想使用 ProgressBar 并且我想设置 平滑的倒计时动画.

我写下面的代码是为了流畅的动画,当设置1000ms (1s) for duration 显示 平滑 animation,但我想设置 10000ms (10s) 持续 duration

当持续时间设置为 10000 毫秒(10 秒)时动画不流畅并显示碎片倒计时。

但我想设置 10000 毫秒 持续时间 并显示平滑倒计时

public class SimpleFrag extends Fragment {

    TextView toolbar_title;
    RelativeLayout toolbar;
    private ProgressBar progressBar;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_simple, container, false);

        ButterKnife.bind(this, view);
        toolbar_title = getActivity().findViewById(R.id.toolbar_title);
        toolbar = getActivity().findViewById(R.id.toolbar);
        progressBar = view.findViewById(R.id.progress);

        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @SuppressLint("ObjectAnimatorBinding")
                @Override
                public void run() {
                    ProgressBarAnimation mProgressAnimation = new ProgressBarAnimation(progressBar, 79, 0);
                    mProgressAnimation.setDuration(10000);
                    progressBar.startAnimation(mProgressAnimation);
                }
            }, 50);
        }
    }

    public class ProgressBarAnimation extends Animation {
        private ProgressBar progressBar;
        private float from;
        private float to;

        public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
            super();
            this.progressBar = progressBar;
            this.from = from;
            this.to = to;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            float value = from + (to - from) * interpolatedTime;
            progressBar.setProgress((int) value);
        }
    }
}

我怎么可以呢?请帮我 。谢谢大家 <3

拿走你现在拥有的,删除你的 setUserVisibleHint() 方法和你的 ProgressBarAnimation class。添加:

private void setUpObserver() {
    progressBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            startAnimation();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                progressBar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            else {
                progressBar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });
}

private void startAnimation() {
    int width = progressBar.getWidth();
    progressBar.setMax(width);

    ValueAnimator animator = ValueAnimator.ofInt(0, width);
    animator.setInterpolator(new LinearInterpolator());
    animator.setStartDelay(0);
    animator.setDuration(10_000);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int value = (int) valueAnimator.getAnimatedValue();
            progressBar.setProgress(value);
        }
    });

    animator.start();
}

然后,在您的 onCreateView() 方法中,在 return view;

之前调用 setUpObserver()

setUpObserver() 方法使用 OnGlobalLayoutListener 来确保系统在开始动画之前等到 ProgressBar 被测量和布局。

startAnimation() 方法负责实际设置和 运行 动画。您可以根据需要修改传递给 setStartDelay()setDuration() 的值。

此解决方案的诀窍是确保进度条的最大值等于其宽度,这意味着 "progress" 的每个增量等于屏幕上的一个像素。 android 框架动画负责确保一切尽可能顺利地运行。

编辑

如果您希望能够控制动画的 start/end 点(而不是仅仅从空变满),请进行以下更改:

startAnimation() 的声明更改为:

private void startAnimation(float startPercent, float endPercent)

从里面删除这一行startAnimation()

ValueAnimator animator = ValueAnimator.ofInt(0, width);

并改为添加这些行:

int start = (int) (startPercent * width);
int end = (int) (endPercent * width);
ValueAnimator animator = ValueAnimator.ofInt(start, end);

最后,通过传递小数百分比来调用startAnimation()

startAnimation(0.79f, 0.10f); // will animate from 79% to 10%