Introduction to Android animation kotlin

preface

Google announced at this year's IO conference that it will replace the official language developed by android with kotlin. As people playing android with Google, we must understand and use kotlin language as soon as possible.

This article will introduce the relevant contents of kotlin, an introduction to Android animation, and share them for your reference and learning. I won't say much below. Let's take a look at the detailed introduction together.

Attribute animation

sketch

It's cool to realize some animation effects on the mobile phone, so the Android system provides us with two ways to realize animation effects at the beginning, frame by frame animation and tweeted animation. The working principle of frame by frame animation is very simple. In fact, it is to split a complete animation into individual pictures, and then play them together. It is similar to the working principle of animation. Make up animation can perform a series of animation operations on the view, including fade in and fade out, zoom, translation and rotation.

However, since Android version 3.0, the system has provided us with a new animation mode, property animation. Its function is very powerful, which makes up for some defects of the previous gap animation and can almost completely replace the gap animation. I don't want to talk more about the usage of frame by frame animation and make-up animation. Their technology is relatively old, and there are many online materials. The theme of our article today is to fully analyze Android attribute animation. Why introduce attribute animation?

The previous patch animation mechanism of Android is actually quite sound. There are many classes under the android.view.animation package for us to operate to complete a series of animation effects, such as moving, scaling, rotating, fading in and out of the view, and we can also combine these animation effects with the help of the animationset, In addition, you can configure interpolator to control the playback speed of animation, and so on. So you may have questions here. Since the previous animation mechanism has been so sound, why introduce attribute animation?

In fact, the so-called soundness above is relative. If you only need to move, zoom, rotate, fade in and fade out the view in your requirements, the gap animation is indeed sound enough. But obviously, these functions are not enough to cover all scenes. Once our needs exceed the four operations of view: move, zoom, rotate and fade in and out, the gap animation can no longer help us. That is to say, it has considerable limitations in function and scalability, So let's take a look at the scenes that make-up animation can't do.

Note: I used the description of "operate on view" when introducing the make-up animation above. Yes, the make-up animation can only act on the view. In other words, we can animate a button, textview, or even LinearLayout, or any other component inherited from view, but if we want to animate an object other than view, sorry, make-up animation won't help. Some friends may feel that they can't understand. How can I animate a non view object? Let me give a simple example. For example, we have a custom view in which a point object is used to manage coordinates, and then the OnDraw () method draws according to the coordinate value of the point object. In other words, if we can animate the point object, the animation effect of the whole custom view will be. Obviously, make-up animation does not have this function, which is its first defect.

Then there is another defect in the gap animation, that is, it can only realize the four animation operations of moving, scaling, rotating and fading in and out. What if we want to dynamically change the background color of view? Unfortunately, we can only achieve it by ourselves. To put it bluntly, the previous gap animation mechanism was completed by hard coding. These are the functional limitations. Basically, there is no scalability.

Finally, there is a fatal flaw in the gap animation, that is, it only changes the display effect of the view, but does not really change the properties of the view. What do you mean? For example, there is a button in the upper left corner of the screen, and then we move it to the lower right corner of the screen through the gap animation. Now you can try to click this button. The click event will never be triggered, because in fact, this button still stays in the upper left corner of the screen, It's just that the make-up animation draws this button to the lower right corner of the screen.

For these reasons, the Android development team decided to introduce the feature of attribute animation in version 3.0. Has attribute animation solved all the above problems? Now let's have a look.

The newly introduced attribute animation mechanism is no longer designed for view, nor is it limited to the animation operations of moving, scaling, rotating and fading in and out. At the same time, it is no longer just a visual animation effect. It is actually a mechanism to continuously operate on the value, and assign the value to the specified attribute of the specified object, which can be any attribute of any object. Therefore, we can still move or zoom a view, but we can also animate the point object in the custom view. We only need to tell the system the running time of the animation, the type of animation to be executed, and the initial and end values of the animation. The rest of the work can be handed over to the system.

Since the implementation mechanism of attribute animation is realized by assigning values to the target object and modifying its attributes, the problem of button display mentioned earlier no longer exists. If we move a button through attribute animation, the button is really moved, and it is no longer just drawn in another position. Well, after so much introduction, I believe you have a basic understanding of attribute animation. Let's start to learn the usage of attribute animation.

Core class

Basic use

This is a demo that lets textview click and move down

Effect display

xml:

kotlin:

This is an unlimited playback and inversion effect that changes the background of textview from 0xf000000 to 0xf00ffff for 3 seconds

Directly modify the code just now to

Finally, another ultimate animation combination

Change the code to:

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>