Depending on how your view gets its background color and how you get your target color there are several different ways to do this.
The first two uses the Android Property Animation framework.
Use a Object Animator if:
- Your view have its background color defined as a
argb
value in a xml file.
- Your view have previously had its color set by
view.setBackgroundColor()
- Your view have its background color defined in a drawable that DOES NOT defines any extra properties like stroke or corner radiuses.
- Your view have its background color defined in a drawable and you want to remove any extra properties like stroke or corner radiuses, keep in mind that the removal of the extra properties will not animated.
The object animator works by calling view.setBackgroundColor
which replaces the defined drawable unless is it an instance of a ColorDrawable
, which it rarely is. This means that any extra background properties from a drawable like stroke or corners will be removed.
Use a Value Animator if:
- Your view have its background color defined in a drawable that also sets properties like the stroke or corner radiuses AND you want to change it to a new color that is decided while running.
Use a Transition drawable if:
- Your view should switch between two drawable that have been defined before deployment.
I have had some performance issues with Transition drawables that runs while I am opening a DrawerLayout that I haven't been able to solve, so if you encounter any unexpected stuttering you might have run into the same bug as I have.
You will have to modify the Value Animator example if you want to use a StateLists drawable or a LayerLists drawable, otherwise it will crash on the final GradientDrawable background = (GradientDrawable) view.getBackground();
line.
Object Animator:
View definition:
<View
android:background="#FFFF0000"
android:layout_width="50dp"
android:layout_height="50dp"/>
Create and use a ObjectAnimator
like this.
final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(view,
"backgroundColor",
new ArgbEvaluator(),
0xFFFFFFFF,
0xff78c5f9);
backgroundColorAnimator.setDuration(300);
backgroundColorAnimator.start();
You can also load the animation definition from a xml using a AnimatorInflater like XMight does in Android objectAnimator animate backgroundColor of Layout
Value Animator:
View definition:
<View
android:background="@drawable/example"
android:layout_width="50dp"
android:layout_height="50dp"/>
Drawable definition:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke
android:color="#edf0f6"
android:width="1dp"/>
<corners android:radius="3dp"/>
</shape>
Create and use a ValueAnimator like this:
final ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(),
0xFFFFFFFF,
0xff78c5f9);
final GradientDrawable background = (GradientDrawable) view.getBackground();
currentAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(final ValueAnimator animator) {
background.setColor((Integer) animator.getAnimatedValue());
}
});
currentAnimation.setDuration(300);
currentAnimation.start();
Transition drawable:
View definition:
<View
android:background="@drawable/example"
android:layout_width="50dp"
android:layout_height="50dp"/>
Drawable definition:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#FFFFFF"/>
<stroke
android:color="#edf0f6"
android:width="1dp"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#78c5f9"/>
<stroke
android:color="#68aff4"
android:width="1dp"/>
<corners android:radius="3dp"/>
</shape>
</item>
</transition>
Use the TransitionDrawable like this:
final TransitionDrawable background = (TransitionDrawable) view.getBackground();
background.startTransition(300);
You can reverse the animations by calling .reverse()
on the animation instance.
There are some other ways to do animations but these three is probably the most common. I generally use a ValueAnimator.