如何在android中将动画平滑设置为progressBar

如何在android中将动画平滑设置为progressBar,android,animation,android-animation,android-progressbar,Android,Animation,Android Animation,Android Progressbar,在我的项目中,我希望使用ProgressBar,并希望设置平滑的倒计时动画 我为平滑动画写了下面的代码,当设置1000ms(1s)持续时间时平滑地显示动画,但我想为持续时间设置10000ms(10s) 设置10000毫秒(10秒)持续时间时,动画不平滑,并显示零碎的倒计时。 但是我想为持续时间设置10000ms,并显示平稳倒计时 public class SimpleFrag extends Fragment { TextView toolbar_title; Relative

在我的项目中,我希望使用
ProgressBar
,并希望设置平滑的倒计时动画

我为平滑动画写了下面的代码,当设置1000ms(1s)持续时间时
平滑地显示
动画
,但我想为持续时间设置10000ms(10s)

设置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);
        }
    }
}

我该怎么做?请帮帮我。谢谢大家拿走你现在拥有的,删除你的
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();
}
然后,在
onCreateView()
方法中,在
返回视图之前调用
setUpObserver()

setUpObserver()
方法使用
OnGlobalLayoutListener
确保系统在开始动画之前等待,直到测量到
ProgressBar
的布局

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

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

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

startAnimation()
的声明更改为:

private void startAnimation(float startPercent, float endPercent)
从内部删除此行
startAnimation()

并添加以下行:

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%

谢谢,但我希望设置“从”和“到”值以显示进度。例如从79开始,以0结束。如何在您的代码中输入它?你能给我发个密码吗?我是业余爱好者,我真的需要你的帮助。你能帮我吗?在你的帮助下,不是倒计时。我想从100开始,到0。但是在你的代码中,从0开始到100!我想从100开始,到0。我该怎么做?请帮助我,我的朋友。谢谢我的朋友,你能在一个完整的代码里把你的编辑代码发给我吗?比如你上面的完整代码。求求你,我的朋友,求求你:(真的,我是业余爱好者,需要你的帮助
startAnimation(0.79f, 0.10f); // will animate from 79% to 10%