Activity Transitions animation very early, but too monotonous, the style is not good to see, so Google after Android5.0, and launched a new transition animation, the effect is very dazzling, today we look together.
1. Old Transition Animation review
First of all, let's take a look at 5.0. What if we want to use animation when we start the activity?
[Java]View Plaincopy print?
- StartActivity (new Intent (This, main3activity. Class));
- Overridependingtransition (r.anim.in,r.anim.out);
The corresponding entry and exit animations are two tweened animations, as follows:
Entrance Animation:
[Java]View Plaincopy print?
- <?xml version="1.0" encoding="Utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- Android:toydelta="0"
- Android:fromydelta="100%"
- android:duration="/>"
- </set>
Appearance Animation:
[Java]View Plain Copy Print?
- <?xml version="1.0" encoding="Utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration=" "
- Android:fromydelta="0"
- Android:toydelta=" -100%"/>
- </set>
This animation is for the entire activity and cannot set the entry/exit animations for elements in the activity. If you want to set the exit/Entry animation for an element in activity, you can do so by setting the Out/Entry animation of elements on the layout. But this has undoubtedly increased the workload.
Transition animations after 2 5.0
Android5.0 after the activity of the field animation in general can be divided into two, one is decomposition, sliding into, fade in, the other is to share the elements of animation, the following we respectively on the two animation description.
2.1 Decomposition, sliding into, fading 1. Decomposition
Let's take a look at the first one:
Is such an effect, let's look at how this effect will be implemented.
First, change the code of the previous activity to the following:
[Java]View Plaincopy print?
- StartActivity (new Intent (This, main2activity. Class), activityoptions.makescenetransitionanimation (this). Tobundle ());
Once added, set the activity's entry and exit animations in main2activity:
[Java]View Plaincopy print?
- GetWindow (). Setentertransition (new Explode (). Setduration (2000));
- GetWindow (). Setexittransition (new Explode (). Setduration (2000));
OK, that's so simple, oh, yes, you must remember to add the following line of code in the Styles.xml file to indicate the transition effect of activating the element in activity:
[Java]View Plaincopy print?
- <item name="android:windowcontenttransitions" >true</item>
2. Slide into
With the above steps, it is easy to set the sliding entry, just modify the two lines of code in the main2activity:
[Java]View Plaincopy print?
- GetWindow (). Setentertransition (new Slide (). Setduration (2000));
- GetWindow (). Setexittransition (new Slide (). Setduration (2000));
The display results are as follows:
3. Fade in and fade out
Main2activity Modify the code as follows:
[Java]View Plaincopy print?
- GetWindow (). Setentertransition (new Fade (). Setduration (2000));
- GetWindow (). Setexittransition (new Fade (). Setduration (2000));
The display results are as follows:
2.2 Sharing element animations
Shared element animation is a very magical thing, let's take a look at the effect:
Perhaps this GIF animation is not very clear, I will explain, in mainactivity and main2activity inside there is a button, but a big one small, from mainactivity jump to Main2activity, I didn't feel the jump to the activity, just felt as if the button on the first page was magnified, and, similarly, when I went back to the first page from the second page, it was as if the button was smaller. OK, this is our activity sharing element.
When you have the same control in the two activity, we can use the shared element animation.
When using the shared elements animation, we need to first add the Android:transitionname= "mybtn" property to the two buttons in mainactivity and main2activity, and the value of the property will be the same. This way the system knows that the two controls are shared elements. After the setup is complete, the next step is to start the activity code, as follows:
[Java]View Plaincopy print?
- StartActivity (new Intent (this,main2activity. Class), Activityoptions.makescenetransitionanimation (this,view,"mybtn"). Tobundle ());
Or the above type of startup method of overloading, but there are more than two parameters, the view represents a shared element in the mainactivity (that is, the button), the second parameter represents the value of the Transitionanimation property in the layout file. OK, that's easy.
When some of the packages may be questionable, what if I have multiple shared elements in two pages? Simply, the Android:transitionname property is set as above, and we can set multiple shared elements through the Pair.create method when the activity is started, as follows:
[Java]View Plaincopy print?
- StartActivity (new Intent (This, main2activity. Class),
- Activityoptions.makescenetransitionanimation (This, Pair.create ((view) iv1),"Myiv"), create ((view) TextView),"MyTV")). Tobundle ());
There are two parameters in the Pair.create method, the first being an instance of the shared element (note that if the view type), the second argument is the value of the view's Transitionanimation property.
As simple as that, cheese try it quickly.
Above.
Transitions Animation for Android5.0 activity