如何在Android中将平滑的动画设置为progressBar [英] How to set smoothly animation to progressBar in android

查看:259
本文介绍了如何在Android中将平滑的动画设置为progressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my project I want use ProgressBar and I want set smoothly countdown animation.

我在下面编写了平滑动画的代码,并在持续时间设置为 1000ms(1s)时显示了平滑 <代码>动画,但我想为持续时间设置 10000毫秒(10秒).

I write below codes for smoothly animation and when set 1000ms (1s) for duration show smoothly animation, but I want set 10000ms (10s) for duration.

当设置持续时间10000ms(10s)时,动画不流畅并显示片段倒数.

但是我想为持续时间设置 10000ms ,并显示平稳倒计时.

But I want set 10000ms for duration and show smoothly countdown.

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

How can I it? please help me . Thanks all <3

推荐答案

现在就做什么,然后删除您的 setUserVisibleHint()方法和您的 ProgressBarAnimation 类.添加此内容:

Take what you have now, and delete your setUserVisibleHint() method and your ProgressBarAnimation class. Add this:

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()方法内部调用 setUpObserver()

Then, inside your onCreateView() method, call setUpObserver() right before you return view;

setUpObserver()方法使用 OnGlobalLayoutListener 来确保系统在开始测量 ProgressBar 之前一直等待动画.

The setUpObserver() method uses an OnGlobalLayoutListener to make sure that the system waits until the ProgressBar is measured an laid out before starting the animation.

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

The startAnimation() method is what takes care of actually setting up and running the animation. You can modify the values passed to setStartDelay() and setDuration() as you see fit.

此解决方案的技巧是确保进度条的最大值等于其宽度,这意味着进度"的每个增量等于屏幕上的一个像素.android框架动画会确保所有内容都尽可能流畅地运行.

The trick to this solution is making sure that the maximum value for the progress bar is equal to its width, meaning that each increment of "progress" is equal to one pixel on the screen. The android framework animation takes care of making sure everything runs as smoothly as possible.

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

If you want to be able to control the start/end points for the animation (rather than just go from empty to full), make these changes:

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():

Finally, call startAnimation() by passing in fractional percentages:

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

这篇关于如何在Android中将平滑的动画设置为progressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆