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

Red*_*nce 2 animation android android-animation android-progressbar

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

我下面写的代码顺利动画,当一套1000毫秒(1秒)时间演出顺利 animation,但我想要一套10000ms(10秒)时间

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

但我想一套10000ms时间,并显示顺利倒计时

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Ben*_* P. 8

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

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();
}
Run Code Online (Sandbox Code Playgroud)

然后,在你的onCreateView()方法中,setUpObserver()在你之前调用return view;

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

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

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

编辑

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

将声明更改为startAnimation()

private void startAnimation(float startPercent, float endPercent)
Run Code Online (Sandbox Code Playgroud)

从里面删除这一行 startAnimation()

ValueAnimator animator = ValueAnimator.ofInt(0, width);
Run Code Online (Sandbox Code Playgroud)

并添加这些行:

int start = (int) (startPercent * width);
int end = (int) (endPercent * width);
ValueAnimator animator = ValueAnimator.ofInt(start, end);
Run Code Online (Sandbox Code Playgroud)

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

startAnimation(0.79f, 0.10f); // will animate from 79% to 10%
Run Code Online (Sandbox Code Playgroud)