Animate change of view background color on Android

前端 未结 16 696
孤街浪徒
孤街浪徒 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:20

    Answer is given in many ways. You can also use ofArgb(startColor,endColor) of ValueAnimator.

    for API > 21:

    int cyanColorBg = ContextCompat.getColor(this,R.color.cyan_bg);
    int purpleColorBg = ContextCompat.getColor(this,R.color.purple_bg);
    
    ValueAnimator valueAnimator = ValueAnimator.ofArgb(cyanColorBg,purpleColorBg);
            valueAnimator.setDuration(500);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                  @Override
                  public void onAnimationUpdate(ValueAnimator valueAnimator) {
                       relativeLayout.setBackgroundColor((Integer)valueAnimator.getAnimatedValue());
                  }
            });
            valueAnimator.start();
    
    0 讨论(0)
  • 2020-11-22 14:23

    If you want color animation like this,

    this code will help you:

    ValueAnimator anim = ValueAnimator.ofFloat(0, 1);   
    anim.setDuration(2000);
    
    float[] hsv;
    int runColor;
    int hue = 0;
    hsv = new float[3]; // Transition color
    hsv[1] = 1;
    hsv[2] = 1;
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
    
            hsv[0] = 360 * animation.getAnimatedFraction();
    
            runColor = Color.HSVToColor(hsv);
            yourView.setBackgroundColor(runColor);
        }
    });
    
    anim.setRepeatCount(Animation.INFINITE);
    
    anim.start();
    
    0 讨论(0)
  • 2020-11-22 14:23

    This is the method I use in a Base Activity to change background. I'm using GradientDrawables generated in code, but could be adapted to suit.

        protected void setPageBackground(View root, int type){
            if (root!=null) {
                Drawable currentBG = root.getBackground();
                //add your own logic here to determine the newBG 
                Drawable newBG = Utils.createGradientDrawable(type); 
                if (currentBG==null) {
                    if(Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN){
                        root.setBackgroundDrawable(newBG);
                    }else{
                        root.setBackground(newBG);
                    }
                }else{
                    TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{currentBG, newBG});
                    transitionDrawable.setCrossFadeEnabled(true);
                    if(Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN){
                         root.setBackgroundDrawable(transitionDrawable);
                    }else{
                        root.setBackground(transitionDrawable);
                    }
                    transitionDrawable.startTransition(400);
                }
            }
        }
    

    update: In case anyone runs in to same issue I found, for some reason on Android <4.3 using setCrossFadeEnabled(true) cause a undesirable white out effect so I had to switch to a solid colour for <4.3 using @Roman Minenok ValueAnimator method noted above.

    0 讨论(0)
  • 2020-11-22 14:25

    You can make an object animator. For example, I have a targetView and I want to change your background color:

    int colorFrom = Color.RED;
    int colorTo = Color.GREEN;
    int duration = 1000;
    ObjectAnimator.ofObject(targetView, "backgroundColor", new ArgbEvaluator(), colorFrom, colorTo)
        .setDuration(duration)
        .start();
    
    0 讨论(0)
提交回复
热议问题