Reputation: 233
I have a very basic selector that I am using as the background for some buttons to achieve down states. Here's the xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@color/home_button_blue_down" android:state_selected="true" />
<item android:drawable="@color/home_button_blue_down" android:state_pressed="true" />
<item android:drawable="@color/home_button_blue" />
</selector>
With this selector the fade animation will occur every time the button changes state. In other words, the transition will animate both when going from de-pressed to pressed and also when going back from pressed to de-pressed.
Now, my question is: is it possible to make it so that only one of these state changes animates? When a user presses the button, I want the downstate transition to occur immediately without animations. When the button becomes de-pressed, I want the downstate to fade out while the normal state fades back in. Can this be done?
Upvotes: 20
Views: 28810
Reputation: 185
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_shortAnimTime"
android:exitFadeDuration="@android:integer/config_longAnimTime">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#917777"
android:angle="270" />
</shape>
</item>
<item android:state_pressed="false" >
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#d2adad"
android:angle="270" />
</shape>
</item>
<item android:state_checked="true">
<color android:color="@color/black"/>
</item>
</selector>
Upvotes: 8
Reputation: 6080
Animations on selector : (my drawables were colors )
De-Press (Out)
android:exitFadeDuration="@android:integer/config_shortAnimTime"
Press (In)
android:enterFadeDuration="@android:integer/config_shortAnimTime"
Full example:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_shortAnimTime"
android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:state_checked="false" android:drawable="@color/transparent"/>
<item android:state_checked="true" android:drawable="@drawable/circle_blue"/>
</selector>
Upvotes: 39
Reputation: 18819
You can do something like this:
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/selected"
android:state_selected="true"
android:drawable="@color/home_button_blue_down"
/>
<item android:id="@+id/usual"
android:drawable="@android:color/transparent"
/>
<transition
android:fromId="@+id/usual"
android:toId="@+id/selected" >
<animation-list>
<!--fill in your animation here-->
</animation-list>
</transition>
</animated-selector>
Keep in mind animated-selector
is available only after API 21.
More info in this official guide
Upvotes: 16