Transition animations for Android activity and fragment

Source: Internet
Author: User

Activity Transitions Animation

The transition animations for activity are implemented by overridependingtransition (int enteranim, int exitanim).

This method is added by API level 5.

This method startActivity(Intent) finish() is called after or, specifying the next transition animation.

The first parameter of the method: Enteranim, is the resource ID of the new activity entering the animation;

The second parameter, Exitanim, is the resource ID of the old activity (current activity) leaving the animation.

So the object of these two parameters is two activity.

If the above two parameters are not animated to be set, then 0 is used as the parameter.

The animated resource file is placed in the res\anim\ directory and is the View Animation.

See this blog post: Android Animation Learning (vi) View animation introduction

The View animation contains basic animation types, which basically meet the needs of general transition animations.

The root node can be:<alpha>, <scale>, <translate>, <rotate>, interpolator element, or <set> .

Set can be used to make a variety of nested combinations.

code example:

New Intent (testactivities.  this, Testactivityfirst. class ); startactivity (intent); // Transaction Animationoverridependingtransition (R.anim.slide_in_bottom,r.anim.slide_out_bottom);

Two of these animations:

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android">     <android:fromydelta= "100%p"  android:toydelta= "0"             android:duration= "/>    " <  android:fromalpha= "0.0"  android:toalpha= "1.0"             Android:duration= "$"/></set>
<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android">     <android:fromydelta= "0%p"  android:toydelta= "100%p"              android:duration= "/>"    <   Android:fromalpha= "1.0"  android:toalpha= "0.0"            android:duration  = "/>"</set>

Fragment Transition Animations

Fragment transition animation implementation is divided into the use of V4 package and do not use V4 package two cases, do not use V4 package, the minimum API level needs to be one.

Standard transition animations:

You can assign a standard transition animation to fragment through the settransition (int transit) method.

The three parameters that the method can pass in are:

Transit_none,

Transit_fragment_open,

Transit_fragment_close

Animations with no animations, open forms, and closed forms are respectively appropriate.

  When the standard animation is set, it will be available when fragment is added and removed.

Custom Transition Animations

Custom transition animations are through the setcustomanimations () method, because fragment can be added to the back Stack , so the transition animations are added, removed, and Pop out of the stack and into four situations.

Note the Setcustomanimations () method must be set before the Add, remove, and replace calls , otherwise it will not work.

android.app.Fragment

Class Reference:

Fragment: http://developer.android.com/reference/android/app/Fragment.html

fragmenttransaction: http://developer.android.com/reference/android/app/FragmentTransaction.html

The type of animation for which the V4 package is not used (min API >=11) is property Animation.

That is, the animation resource file needs to be placed in the res\animator\ directory, and the root tag is <set>, <objectanimator>, or <valueAnimator> one of the three.

This can also be seen from this method in fragment: oncreateanimator (int Transit, boolean enter, int nextanim), and the return value is animator.

When customizing a transition animation, the four Parameters form setcustomanimations (int enter, int exit, int popenter, int popexit) are API level, 11 introduces only two animations, that is, you cannot specify a transition animation when the back stack stack operation is in action.

code example:

    Private voidaddfragment () {if(NULL==Mfragmentmanager) {Mfragmentmanager=Getfragmentmanager (); } Mtextfragmentone=NewMyfragmentone (); Fragmenttransaction fragmenttransaction=Mfragmentmanager. BeginTransaction (); //Standard animations//fragmenttransaction//. Settransition (Fragmenttransaction.transit_fragment_open); //fragmenttransaction//. Settransition (Fragmenttransaction.transit_fragment_fade); //fragmenttransaction//. Settransition (Fragmenttransaction.transit_fragment_close); //Custom Animations//API Level Onefragmenttransaction.setcustomanimations (R.animator.fragment_slide_left_enter,        R.animator.fragment_slide_right_exit); //API Level//Fragmenttransaction.setcustomanimations (//R.animator.fragment_slide_left_enter,//R.animator.fragment_slide_left_exit,//R.animator.fragment_slide_right_enter,//r.animator.fragment_slide_right_exit);Fragmenttransaction.add (R.id.container, Mtextfragmentone); //Add to BackstackFragmenttransaction.addtobackstack (NULL);    Fragmenttransaction.commit (); }

Four of these animations were taken from the Apidemos:

Fragment_slide_left_enter:

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "100DP"Android:valueto= "0DP"Android:valuetype= "Floattype"Android:propertyname= "Translationx"android:duration= "@android: Integer/config_mediumanimtime" />    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "0.0"Android:valueto= "1.0"Android:valuetype= "Floattype"Android:propertyname= "Alpha"android:duration= "@android: Integer/config_mediumanimtime" /></Set>

Fragment_slide_left_exit:

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "0DP"Android:valueto= " -100DP"Android:valuetype= "Floattype"Android:propertyname= "Translationx"android:duration= "@android: Integer/config_mediumanimtime" />    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "1.0"Android:valueto= "0.0"Android:valuetype= "Floattype"Android:propertyname= "Alpha"android:duration= "@android: Integer/config_mediumanimtime" /></Set>

Fragment_slide_right_enter:

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= " -100DP"Android:valueto= "0DP"Android:valuetype= "Floattype"Android:propertyname= "Translationx"android:duration= "@android: Integer/config_mediumanimtime" />    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "0.0"Android:valueto= "1.0"Android:valuetype= "Floattype"Android:propertyname= "Alpha"android:duration= "@android: Integer/config_mediumanimtime" /></Set>

Fragment_slide_right_exit:

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "0DP"Android:valueto= "100DP"Android:valuetype= "Floattype"Android:propertyname= "Translationx"android:duration= "@android: Integer/config_mediumanimtime" />    <ObjectanimatorAndroid:interpolator= "@android: Interpolator/decelerate_quint"Android:valuefrom= "1.0"Android:valueto= "0.0"Android:valuetype= "Floattype"Android:propertyname= "Alpha"android:duration= "@android: Integer/config_mediumanimtime" /></Set>

android.support.v4.app.Fragment

Fragment: http://developer.android.com/reference/android/support/v4/app/Fragment.html

fragmenttransaction: http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html

With the V4 package, the use of fragment is no longer limited to API level 11, and low-grade APIs are also available, but the type of transition animation is View Animation.

The animation resource is placed under the res\anim\ path, just like the transition animation for the activity.

Methods in Fragment: oncreateanimation (int Transit, boolean enter, int nextanim) return a value of animation.

The Setcustomanimations () method in Fragmenttransaction, both parameter types and four parameter types are available.

So generally still use V4 package of this version, one is the compatibility is good, in addition view animation actually basically can meet the need of transition animation.

code example:

    Private void addfragment () {        if (null = = Mfragmentmanager)            {=  Getsupportfragmentmanager ();        }         New Myfragmentone ();         = Mfragmentmanager                . BeginTransaction ();        Fragmenttransaction.setcustomanimations (                r.anim.push_left_in,                r.anim.push_left_out,                R.anim.push _left_in,                r.anim.push_left_out);        Fragmenttransaction.add (R.id.container, mtextfragmentone);        Fragmenttransaction.addtobackstack (null);        Fragmenttransaction.commit ();    }

Demo:

Complete Example Project:https://github.com/mengdd/HelloTransactionAnimations

Original: http://www.cnblogs.com/mengdd/p/3494041.html

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.