Android property animation principle and databinding

Android property animation principle and databinding

When you see this title, you may wonder what is the relationship between attribute animation and databinding? My personal understanding is that their internal implementation ideas are similar. This article mainly arranges and records the knowledge of Android attribute animation through text. For the content, refer to "exploration of Android development art". Finally, I will give the reasons for my understanding of attribute animation and databinding.

Android animation overview:

Android animation can be divided into three types: View animation, frame animation and attribute animation. View animation produces animation effects by constantly changing the image of objects in the scene (translation, scaling, rotation and transparency). View animation supports customization. Frame animation produces animation effects by playing a series of images in sequence, which can be simply understood as picture switching animation. Too many pictures will lead to oom. Attribute animation achieves the animation effect by dynamically changing the attributes of the object. Attribute animation is a new feature of API 11. In the lower version, it needs to be used through the compatibility library.

Attribute Animation:

Attribute animation can animate the attributes of any object, not just the view. The object can change from one attribute value to another within a time interval. Therefore, attribute animation is almost omnipotent. As long as an object has this attribute, it can achieve animation effect

use:

There are concepts such as valueanimator, objectanimator and animatorset in attribute animation. Objectanimator inherits from valueanimator and is used to implement an attribute animation. Animatorset is an animation set, which can define a group of animations. They are extremely simple to use. There is no example here. In addition to code implementation, attribute animation, like view animation, can also be defined through XML. Attribute animation needs to be defined in RES / Animator directory, where set, objectanimator and animator tags correspond to animatorset, objectanimator and valueanimator respectively. Although attribute animation can be realized through XML, code is often used in actual development, because the starting value of an attribute can not be determined in advance.

Interpolator and estimator:

Timeinterpolator is translated into time interpolator. Its function is to calculate the percentage of change in the current attribute value according to the percentage of time elapsed. The system presets linear interpolator (linear interpolator: uniform animation), accelerated decelerateinterpolator (accelerated decelerateinterpolator: slow at both ends and fast in the middle of animation) and decelerateinterpolator (deceleration interpolator: animation is getting slower and slower) and so on.

Typeevaluator is translated into type evaluation algorithm, also known as evaluator. Its function is to calculate the changed attribute value according to the percentage of current attribute change. The system presets intevaluator (for integer attribute), floatevaluator (for floating-point attribute) and argevaluator (for color attribute). Interpolator and type evaluator are very important in attribute animation. They are important means to realize non-uniform animation.

Listener for attribute Animation:

Attribute animation provides listeners to monitor the playback process of animation, mainly animatorupdatelistener and animatorlistener. Animatorlistener can monitor the beginning, end, cancellation and repeated playback of animation. At the same time, in order to facilitate development, the system also provides the class animatorlistener adapter, which is the adapter class of animatorlistener. You can selectively implement these four monitoring methods. Animatorupdatelistener is special. It will monitor the whole animation process. The animation is composed of many frames. The corresponding monitoring method will be called every time a frame is played.

Animate any attribute:

Attribute animation can add animation effects to any attribute. If we want to add an animation to a button and increase the width of the button by 100px, we may write as follows:

However, after clicking the button, there is no effect. It is not said that any attribute can be animated. In fact, it is right that there is no effect. The principle of attribute animation is analyzed below: attribute animation requires the object to be animated to provide the get and set methods of the attribute, and attribute animation transmits the initial value and final value of the attribute according to the external boundary, Call the set method several times with the animation effect, and the value passed to the set method is different each time. Specifically, the passed value is closer and closer to the final value over time. To sum up, we animate the ABC attribute of the object. If we want the animation to take effect, we must meet the following two conditions at the same time:

1. The object must provide setabc method. If the initial value is not passed during animation, the getabc method must be provided because the system needs to get the initial value of ABC attribute (if this item is not satisfied, the program will crash directly)

2. The changes made by setabc of object to the attribute ABC must be reflected in some way, such as UI changes (if this item is not satisfied, the animation will have no effect but will not crash)

This is the same as the idea in databinding. The object here corresponds to the ViewModel in databinding. The member variable set method of ViewModel needs to remind databinding to update the content of the view, as follows:

To solve the above problems, the official documents tell us that there are three solutions:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>