Android animation. Part 2. Animator and State View

in #android4 years ago

In the last article we discussed how to work with simple animations using Animator. Today let's summarize and consider how you can automate the startup animations.

Simple animation. Animator is the first article
Simple animation. State View - the second
Vector animation. Standard solutions – the third article
VectorDrawable animation. Controls the animation. Hardcore – the fourth article

For this we will need to create a class to manage animations when changing the state of the view. What this includes? For example, the element has been turned off (enable) selected (selected), it will get a focus (focused), down (pressed) and the other States. The only disadvantage of this method, it will work only on versions of Android 5 (API 21) or higher.

Let's get started. Create a file state_animator.xml in the res/animator. Inside we put the code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <set android:ordering="together">
            <objectAnimator
                android:duration="250"
                android:interpolator="@android:anim/decelerate_interpolator"
                android:propertyName="scaleX"
                android:valueTo="1"
                android:valueType="floatType"/>
            <objectAnimator
                android:duration="250"
                android:interpolator="@android:anim/decelerate_interpolator"
                android:propertyName="scaleY"
                android:valueTo="1"
                android:valueType="floatType"/>
        </set>
    </item>
    <item
        android:state_enabled="false">
        <set android:ordering="together">
            <objectAnimator
                android:duration="250"
                android:interpolator="@android:anim/accelerate_interpolator"
                android:propertyName="scaleX"
                android:valueTo="0"
                android:valueType="floatType"/>
            <objectAnimator
                android:duration="250"
                android:interpolator="@android:anim/accelerate_interpolator"
                android:propertyName="scaleY"
                android:valueTo="0"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

What did we do? We pointed out that the marking selector – that is, inside we have described the animations for different States. Inside we have two item for the state state_enabled="false" and state_enabled="true", that is the animation, if the picture is off and the picture is included. Inside the item is already familiar to us from the last article objectAnimator (have you tried something to animate already? I hope so).

What do we do now? We need somewhere to use state_animator.xml. Where? In the markup file of the activity or fragment. In my case activity_main.xml. Where we are looking for is a picture and add there one line:

<ImageView
    …
    android:stateListAnimator="@animator/state_animator"
    … />

Now you also need to change the state of the imageView. Do it in code when the button is pressed. Track press and insert there one line of code (I hope you know how to do it, if not write comments and I will do a separate article):

imageView.setEnabled(!imageView.isEnabled());

Look at the result?

Conclusions:
With Animator, we can animate any parameter objects, and some of them have friendly interface. The lack of such animations, only one – to some extent they are demanding.

Sort:  

Congratulations @mstorm! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Coin Marketplace

STEEM 0.29
TRX 0.06
JST 0.039
SBD 3.89