Example of transition animation of activity in Android 5.0

The transition animation of activity has existed for a long time, but it is too monotonous and the style is not good-looking. Therefore, Google launched a new transition animation after Android 5.0. The effect is still very dazzling. Let's take a look today.

1. Review of old transition animation

First, let's take a look at what we should do if we want to use animation when starting an activity before 5.0?

The corresponding entry and exit animations are two make-up animations, as follows:

Entrance Animation:

Exit Animation:

This kind of animation is for the whole activity, and the entry / exit animation of elements in the activity cannot be set. If you want to set the entry / exit animation of the elements in the activity, you can set the entry / exit animation of the elements in the layout. But this undoubtedly increases the workload.

2 transition animation after 5.0

After Android 5.0, the entry and exit animation of activity can be divided into two types: decomposition, sliding in and fading out, and shared element animation. We will explain these two kinds of animation respectively below.

2.1 break down, slide in, fade out

1. Disassembly

Let's take a look at a rendering:

This is such an effect. Let's see how to achieve this effect.

First, change the code that started the activity to the following:

After adding, set the entry and exit animation of the activity in main2activity:

OK, it's that simple. Oh, by the way, you must remember to add the following line of code in the styles.xml file to indicate the transition effect of activating the elements in the activity:

2. Slide in

With the above steps, it is easy to set sliding entry. You only need to modify two lines of code in main2activity:

The display effect is as follows:

3. Fade in and out

Main2activity modification code is as follows:

The display effect is as follows:

2.2 shared element animation

Shared element animation is a very magical thing. Let's take a look at the effect first:

Maybe the GIF animation is not very clear. Let me explain again. There is a button in both mainactivity and main2activity, but one large and one small. When I jump from mainactivity to main2activity, I don't feel the jump of activity, but I feel as if the button on the first page is enlarged. Similarly, when I return to the first page from the second page, It also seems that the button has become smaller. OK, this is our activity shared element.

When two activities have the same control, we can use shared element animation.

When using shared element animation, we need to add the Android: transitionname = "mybtn" attribute to the two buttons in mainactivity and main2activity respectively, and the value of the attribute should be the same, so that the system can know that the two controls are shared elements. After setting, the code to start the activity is as follows:

It is also the overload method of the above startup mode, but there are two more parameters here. View represents the shared element in mainactivity (that is, the button), and the second parameter represents the value of the transitionanimation attribute in the layout file. OK, that's it.

At this time, some silos may be in doubt. What should I do if there are multiple shared elements in two pages? Simply, the Android: transitionname attribute is also set as above. Then, when starting the activity, we can set multiple shared elements through the pair.create method, as follows:

There are two parameters in the pair.create method. The first parameter is the instance of the shared element (note if it is a view type), and the second parameter is the value of the transitionanimation attribute of the view.

It's that simple. Let's try it quickly.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>