Android - selector selection animation - only animation to delete, DO NOT click

I have a very simple selector that I use as the background for some buttons to achieve states. Here is 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, a fade animation will appear every time the button changes. In other words, the transition will come to life both during the transition from depression and when pressed, as well as when returning from the pressed to the pressed.

Now, my question is: is it possible to make only one of these state changes animate? When the user clicks the button, I want the down-down transition to happen immediately without animation. When the button is pressed, I want the downstream device to disappear when the normal state fades. Can this be done?

+4
source share
2 answers

Selector animation: (my drawings 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>
+6
source

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 that it animated-selectoris only available after API 21.

Further information in this official guide.

+4
source

All Articles