Animate change of view background color on Android

前端 未结 16 982
孤街浪徒
孤街浪徒 2020-11-22 13:23

How do you animate the change of background color of a view on Android?

For example:

I have a view with a red background color. The background color of the

16条回答
  •  野性不改
    2020-11-22 14:06

    Based on ademar111190's answer, I have created this method the will pulse the background color of a view between any two colors:

    private void animateBackground(View view, int colorFrom, int colorTo, int duration) {
    
    
        ObjectAnimator objectAnimator = ObjectAnimator.ofObject(view, "backgroundColor", new ArgbEvaluator(), colorFrom, colorTo);
        objectAnimator.setDuration(duration);
        //objectAnimator.setRepeatCount(Animation.INFINITE);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                // Call this method again, but with the two colors switched around.
                animateBackground(view, colorTo, colorFrom, duration);
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
    
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
    
            }
        });
        objectAnimator.start();
    }
    

提交回复
热议问题