Operation principle of view animation analysis of decorview and viewrootimplandroid view of Android view drawing process detailed explanation of measure process (I) detailed explanation of layout and draw process of Android view drawing process (II)

At present, the Android platform provides two categories of animation. Before Android 3.0, one category was view animation, including tween animation and frame animation. A new animation system, property animation, was introduced into Android 3.0. This article mainly introduces the operation principle of view animation.

View animation enables the view to perform gap animation. That is, given two key frames, and then the middle part automatically calculates the supplement according to a certain algorithm.

Inheritance relationship between complementary animations:

Let's take a look at an example. For example, we want to make a change to transparency:

So how to realize this animation? The following is about its implementation principle.

In order to explore the implementation principle of make-up animation, it is necessary to interpret the relevant source code. The source code version is Android API 29 platform. Before reading, you can try to answer the following questions.

Let's start the source code analysis. First, look at the base class animation.

Animation is an abstract class, which contains various animation related attributes (duration, start time, repetition times, interpolator, etc.) and callbacks (listeners). This class is relatively simple as a whole. Just look at the source code directly.

Let's take a look at the animation subclass. For convenience, we will only talk about alphaanimation this time.

The whole code is also very simple. In fact, a lot of logic is processed in the base class. Then subclasses only need to rewrite some methods related to their own animation. Applytransformation is the key to achieve a certain animation, and each subclass must be overridden.

It should be noted here that transformation is used to store the parameters of each animation. Translation, rotation and scaling are achieved by changing the matrix, while transparency is achieved by changing the alpha value.

In order to facilitate further understanding, you can take a look at the animationset.

Because animationset is a collection of several other subclasses, you can find some differences by looking at its code logic. There are many internal codes, so they won't be posted. Just pick one part:

The above are two important methods in animationset:

Earlier, I introduced some background knowledge of animation. Here, we will have some understanding. Next, analyze the execution of the animation according to the call process.

In short, startanimation mainly does the following things:

ViewGroup.invalidateChild

Let's take a look at how invalidchild promotes redrawing:

Here's the main thing, finding rooView and calling the invalidateChildInParent method. The rootview here is actually viewrootimpl. As for why it is not decorview, you can take a look at this article:

As you can see here, the invaliderectonscreen method will eventually be called.

Invaliderectonscreen is mainly to integrate the dirty matrix with the existing one, and then see whether it is necessary to initiate a refresh.

So where did you start drawing in the view drawing process? The answer starts in the draw method of view.

However, this draw is not a common draw method when we customize the view. The draw method has three parameters and is used for drawing the view itself.

This method is relatively long. In terms of interception:

First, let's look at the three parameters of the draw method:

As soon as you enter the draw method, you first obtain whether hardware acceleration is currently supported. There are two sets of logic with and without hardware acceleration. Then get the animation stored before the warranty.

Then call applylegacyanimation to start processing animation related logic. Let's look at the logic inside its method.

In fact, this method is also very simple to understand. It is mainly to obtain a transformation instance calculated according to the current time, which contains the information required for the next animation.

The contents of transformation are as follows:

The above code also omits many methods, which are actually operations on the matrix.

Let's mention here: the setrotate () method in the matrix method will clear the matrix first, that is, set it as the identity matrix. Then set the rotation operation. Similarly, settranslate() and other methods are the same. So you can't stack various effects together. If you want to use multiple effects at the same time, use matrix transformation methods such as postrotate(), posttranslate().

You can read the code directly if you want to learn more.

Let's talk about how to obtain transformation.

Gettransformation is mainly used to manage the animation state. Whether it starts (record the start time), is it in progress (calculate the progress), or is it over (the notification is over).

The applyTransformation that we call, we need to implement each Animation sub class. Then we can rewrite this method and let the progress flow out, and you'll see. Subclasses actually save the final calculation results in transformation, so as to get the animation parameters of the next frame.

In addition, the animationlistener you usually use is also used for notification callback here.

Then, how to use the transformation after you get it, you have to go back to the view.draw method.

As mentioned earlier, transformation is actually obtained from parent and assigned to transformtoapply;

So how is the animationset handled?

First, it also inherited animation. Second, it has an array door to store the animation collection. Transformation is also obtained through gettransformation.

AnimationSet.

Through the for loop, the corresponding annotation matrix is obtained in turn, and then the matrix effects are combined.

At this point, we should have our own understanding of animation. The whole execution logic of view animation is finished.

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