Android animation learning notes

When using objectanimator, it is very important that the manipulated properties have get and set methods, otherwise objectanimator will not work. The following are some common things that can directly use the attribute values of attribute animation. translationX ,translationY,scaleX,ScaleY,pivotX,pivotY,x,y,alpha。

However, some properties do not have a get set method, so can't we use it? no, it isn't.

Similarly, if you want to implement animationset in view animation, you can use propertyvaluesholder in property animation.

example:

Show only the code in the activity. After clicking, it will fade and pop up four icons in four directions. Click again to retract the four icons.

There are four kinds of view Animation: translation, rotation, scaling and transparency. View animation should avoid interaction, but it is efficient and easy to use. Here is a specific example:

XML file

Five buttons are set, and each button is set with the click method.

Code in activity: note that the click method passes in view, so it can be processed directly.

Valueanimator itself does not provide any animation effect. It is more like a numerical generator, which is used to generate numbers with certain rules, so that the caller can control the implementation of animation.

A complete animation has four processes: start, repeat, end and cancel. Android provides an interface to conveniently listen to these four events. The code is as follows.

Of course, most of the time, we only care about the onanimationend event, so Android also provides an animatorlisteneradapter to let us select the necessary things to listen to. The code is as follows:

example:

After clicking, a drop-down item will pop up, and click again to disappear. It is mainly realized through valueanimator. Through the change of the middle value, the change of its height is set to achieve the effect of animation. We monitor the end of the animation and then set it so that the item is invisible.

Another example, timer, reflects the change of value on the interface.

This attribute can accurately control the order of occurrence of. Other methods are: animset. Play(). With(), playsequentially(), playtogether(), before(), after().

One is XML for attribute animation and the other is frame animation.

Frame animation is to play a set of predefined pictures in sequence, similar to movie playing. Unlike view animation, the system provides another class animationdrawable to use frame animation. Frame animation is relatively simple. First, you need to define an animationdrawable through XML, as shown below.

File address: RES / drawable / frame_ test.xml

Then take the above drawable as the background of view and play the animation through drawable:

Frame animation is relatively simple, but it is easy to cause oom (insufficient memory), so when using frame animation, try to avoid using too many large pictures.

Creating a custom animation is very simple. You only need to implement its applytransformation logic. Usually, however, you need to override the initialize method of the parent class to do some initialization.

Applytransformation has two parameters. The first is the event factor of the interpolator, with a value range of 0-1.

The second parameter is the encapsulation class of the matrix, which is generally used to obtain the current matrix object. The animation is realized by changing the obtained Matrix object.

Two examples: one is the TV off animation, and the other is the custom 3D custom animation effect.

In the 3D custom animation effect, we use a camera, which is the camera class in android.graphics.camera. It encapsulates the 3D animation of OpenGL, so it is very convenient to create 3D animation effect.

Content of activity file:

XML file:

There are mainly two buttons.

Layoutanimation acts on the ViewGroup to make an animation for the ViewGroup, so that it will have this animation effect when its child elements appear. This effect is often used on listviews.

Layoutanimation is also a view animation. In order to add appearance effect to ViewGroup sub elements, you should follow the following steps.

Define layoutanimation as follows:

Android: delay indicates the event delay of the sub animation. For example, if the admission animation cycle is 300ms, then 0.5 means that each sub element has to be delayed by 150ms to play the admission animation.

Android: animationorder indicates the playback order of sub animations. There are three options: normal, reverse and random.

Android: animation means making specific animation for child elements.

Next, specify the Android: layoutanimation attribute for the ViewGroup: Android: layoutanimation = "@ anim / anim_layout". In this way, its child elements will have appearance animation. As follows:

In addition to specifying layoutanimation using XML, it can also be implemented through layoutanimationcontroller. The specific code is as follows.

Activity has the default switching effect, but we can customize this effect, which is mainly used

Overridependingtransition (int enteranim, int exitanim) this method must be

Only after startactivity or finish() is called can it take effect. The meaning of its parameters is as follows:

Enteranim: the animation resource ID required when the activity is opened;

Exitanim: the animation resource ID required when the activity is closed;

When starting an activity, you can add custom effects as follows:

When activity is launched, you can also add custom switching effects in the following ways:

1. Oom problem: it mainly occurs in frame animation. If there are many and large pictures, it is very easy to appear oom;

2. Memory leak: in attribute animation, there is a kind of infinite loop animation, which should be stopped in time when the activity exits; Generally, there is no such problem in view animation;

3. Compatibility: the system with animation below 3.0 has compatibility problems, so it is necessary to do a good job of adaptation;

4. View animation is to animate the impact of view, not really change the state of view. Therefore, sometimes the view cannot be hidden after the animation is completed, that is, setvisibility (view. Gone) fails. At this time, you can solve this problem by calling view. Clearanimation() to clear the view animation.

5. Do not use PX: try to use DP,

6. Interaction of animation elements: in systems below 3.0, single machine events are still triggered at the old position after the animation is completed; Starting from 3.0, the off-season event of attribute animation is triggered to the position after moving, and the view animation remains unchanged.

7. Hardware acceleration: during use, it is recommended to turn on hardware acceleration to improve the fluency of animation.

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