Animate change of view background color on Android

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

    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.

提交回复
热议问题