Android animation. Part 2. Animator and State View

in #android7 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.

<p dir="auto">Simple animation. Animator is the <a href="https://steemit.com/android/@mstorm/android-animation-part-1-animator" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">first article<br /> Simple animation. State View - the <a href="#" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">second<br /> Vector animation. Standard solutions – the <a href="#" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">third article<br /> VectorDrawable animation. Controls the animation. Hardcore – the <a href="#" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">fourth article <p dir="auto">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 <strong>only disadvantage of this method, it will <strong>work only on versions of Android 5 (API 21) or higher. <p dir="auto">Let's get started. Create a file state_animator.xml in the res/animator. Inside we put the code: <pre><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> <p dir="auto">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). <p dir="auto">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: <pre><code><ImageView … android:stateListAnimator="@animator/state_animator" … /> <p dir="auto">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): <pre><code>imageView.setEnabled(!imageView.isEnabled()); <p dir="auto">Look at the result?<br /><br /> <strong>Conclusions:<br /> 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.<span> <a href="https://gateway.ipfs.io/ipfs/QmbH1DWmvFyEAfdQpH6ogFGPByYfg4ZxUeGDXZrkqExowQ" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://gateway.ipfs.io/ipfs/QmbH1DWmvFyEAfdQpH6ogFGPByYfg4ZxUeGDXZrkqExowQ
Sort:  

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

<p dir="auto"><a href="http://steemitboard.com/@mstorm" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 2x" /> Award for the number of upvotes <p dir="auto">Click on any badge to view your own Board of Honor on SteemitBoard.<br /> For more information about SteemitBoard, click <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">Upvote this notification to help all Steemit users. Learn why <a href="https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here!