Skip to content

State List Color Animation

Weiping Huang edited this page Apr 6, 2017 · 3 revisions

Similar to WoWoShapeColorAnimation, WoWoStateListColorAnimation changes the background drawable color of a view. Notice that the drawable must be StateListDrawable, which means the resource file of the drawable should look like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="oval"  >
            <solid android:color="@color/red" />
        </shape>
    </item>

    <item >
        <shape android:shape="oval"  >
            <solid android:color="@color/blue_4"/>
        </shape>
    </item>
</selector>

Then in activity file:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addAnimations(findViewById(R.id.test1), Chameleon.RGB);
    addAnimations(findViewById(R.id.test2), Chameleon.HSV);
}

private void addAnimations(View view, Chameleon chameleon) {
    ViewAnimation viewAnimation = new ViewAnimation(view);
    viewAnimation.add(WoWoStateListColorAnimation.builder().page(0)
            .from("#ff0000", "#ffff00").to("#ffff00", "#00ff00").chameleon(chameleon).build());
    viewAnimation.add(WoWoStateListColorAnimation.builder().page(1)
            .from("#ffff00", "#00ff00").to("#000000", "#0000ff").chameleon(chameleon).build());
    viewAnimation.add(WoWoStateListColorAnimation.builder().page(2)
            .from("#000000", "#0000ff").to("#ff0000", "#ffff00").chameleon(chameleon).build());
    viewAnimation.add(WoWoStateListColorAnimation.builder().page(3).start(0).end(0.5)
            .from("#ff0000", "#ffff00").to("#000000", "#0000ff").chameleon(chameleon).build());
    viewAnimation.add(WoWoStateListColorAnimation.builder().page(3).start(0.5).end(1)
            .from("#000000", "#0000ff").to("#ff0000", "#ffff00").chameleon(chameleon).build());
    wowo.addAnimation(viewAnimation);
    wowo.setEase(ease);
    wowo.setUseSameEaseBack(useSameEaseTypeBack);
    wowo.ready();
}

As mentioned in TextView TextColor Animation, every animation that extends from SingleColorPageAnimation and MultiColorPageAnimation has a parameter named Chameleon which performs the discoloration job. Check Chameleon Wiki to know about the difference between RGB and HSV discoloration-styles and how they works.

WoWoStateListColorAnimation extends MultiColorPageAnimation and PageAnimation. Check more implementation details in its superclasses.

Methods List of WoWoStateListColorAnimation.builder()

Clone this wiki locally