Reputation: 987
I want to make an animated drawable that animates while selected/unselected or not. There are different animation for "selected" -> "not selected" and "not selected" -> "selected" My problem is when i select my view, it triggers the right animation, but when i unselect it triggers the same animation in reverse instead of the correct one. Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- provide a different drawable for each state-->
<item
android:id="@+id/not_selected"
android:state_selected="false"
android:drawable="@drawable/vector_bars" />
<item
android:id="@+id/selected"
android:state_selected="true"
android:drawable="@drawable/vector_arrow" />
<!-- specify a transition -->
<transition
android:fromId="@id/selected"
android:toId="@id/not_selected"
android:drawable="@drawable/anim_arrow_to_bars"
android:reversible="false"/>
<transition
android:fromId="@id/not_selected"
android:toId="@id/selected"
android:drawable="@drawable/anim_bars_to_arrow"
android:reversible="false"/>
</animated-selector>
Upvotes: 0
Views: 1033
Reputation: 987
Ok, after a more extensive search i found an answer. I added 2 additional states and changed the transitions a bit and it works as intended now.
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- provide a different drawable for each state-->
<item
android:id="@+id/not_selected_pressed"
android:state_selected="false"
android:state_pressed="true"
android:drawable="@drawable/vector_bars" />
<item
android:id="@+id/selected_pressed"
android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/vector_arrow" />
<item
android:id="@+id/not_selected"
android:state_selected="false"
android:drawable="@drawable/vector_bars" />
<item
android:id="@+id/selected"
android:state_selected="true"
android:drawable="@drawable/vector_arrow" />
<!-- specify a transition -->
<transition
android:fromId="@id/selected_pressed"
android:toId="@id/not_selected"
android:drawable="@drawable/anim_arrow_to_bars"
android:reversible="false"/>
<transition
android:fromId="@id/selected_pressed"
android:toId="@id/selected"
android:drawable="@drawable/anim_bars_to_arrow"
android:reversible="false"/>
</animated-selector>
Upvotes: 2