Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - State Selector Animations - Only animate de-press, NOT press

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?

like image 575
Reid Conner Avatar asked Nov 07 '14 22:11

Reid Conner


People also ask

What is the difference between ValueAnimator and ObjectAnimator?

As shown in code above, ValueAnimator is almost like ObjectAnimator , except it doesn't have a specific view to target. It has to have a listener — i.e. addUpdateListner — to listen to the value change and behave accordingly. This added more code, but better customization for it.

What is Tweened animation in Android?

Tween Animation takes some parameters such as start value , end value, size , time duration , rotation angle e.t.c and perform the required animation on that object. It can be applied to any type of object. So in order to use this , android has provided us a class called Animation.


1 Answers

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>
like image 106
Jakub S. Avatar answered Oct 11 '22 13:10

Jakub S.