Transitions Animation for Android5.0 activity

Source: Internet
Author: User
Tags explode

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?
    1. StartActivity (new Intent (This, main3activity.   Class));
    2. 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?
    1. <?xml version="1.0" encoding="Utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <translate
    4. Android:toydelta="0"
    5. Android:fromydelta="100%"
    6. android:duration="/>"
    7. </set>


Appearance Animation:

[Java]View Plain Copy Print?
    1. <?xml version="1.0" encoding="Utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <translate
    4. android:duration=" "
    5. Android:fromydelta="0"
    6. Android:toydelta=" -100%"/>
    7. </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?
    1. 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?
    1. GetWindow (). Setentertransition (new Explode (). Setduration (2000));
    2. 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?
    1. <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?
    1. GetWindow (). Setentertransition (new Slide (). Setduration (2000));
    2. 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?
    1. GetWindow (). Setentertransition (new Fade (). Setduration (2000));
    2. 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?
    1. 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?
    1. StartActivity (new Intent (This, main2activity. Class),
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.