Recently many people come to fragment animation is very interested, I will be a sample for everyone to see.
Since doing, I will do the following types of animations:
Inject pop-up animation: Enter from "". From "Up and down" pop up, of course, how you can combine all. In addition you can add some transparency changes, which depends on your play.
。。
1. xml file that writes animation first
Do the development of all know. Under the/res/anim/folder, create a new animation file for the XML. Example:
Fragment_slide_in_from_bottom.xml
<?XML version= "1.0" encoding= "Utf-8"? ><translate xmlns:android= "Http://schemas.android.com/apk/res/android" android:duration= " android:fromydelta=" 100.0%p " android:interpolator=" @android: anim/ Decelerate_interpolator " android:toydelta=" 0.0 "/>
Fragment_slide_in_from_left.xml
<?XML version= "1.0" encoding= "Utf-8"? ><translate xmlns:android= "Http://schemas.android.com/apk/res/android" android:duration= " android:fromxdelta=" -100.0%p " android:interpolator=" @android: anim/ Decelerate_interpolator " android:toxdelta=" 0.0 "/>
Fragment_slide_in_from_right.xml
<?xml version= "1.0" encoding= "Utf-8"? ><translate xmlns:android= "http://schemas.android.com/apk/res/ Android " android:duration=" " android:fromxdelta=" 100.0%p " android:interpolator=" @android: anim/ Decelerate_interpolator " android:toxdelta=" 0.0 "/>
Fragment_slide_in_from_top.xml
<?xml version= "1.0" encoding= "Utf-8"?><translate xmlns:android= "http://schemas.android.com/apk/res/android" android:duration= " Android:fromydelta= " -100.0%p" android:interpolator= "@android: Anim/decelerate_interpolator" android: Toydelta= "0.0"/>
Above is the entry animation. As for pop-up animations, you just need to flip the values from and to. You all know, do not know, go directly to GitHub clone, the address is below.
2. When adding fragment, use the Setcustomanimations method.
Directly paste the code. Simple and clear.
Package Com.example.testfragment;import Android.os.bundle;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmenttransaction;import Android.support.v7.app.actionbaractivity;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import android.widget.button;/** * * @author Zheng Haibo * @web http://www.mobctrl.net * */public class mainactivity Ext Ends Actionbaractivity {Private Fragmentmanager fragmentmanager;private button northbtn;private button southbtn; Private button eastbtn;private button westbtn;private button popbtn; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); FragmentManager = Getsupportfragmentmanager (); Initbutton ();} private void Initbutton () {northbtn = (button) Findviewbyid (R.id.btn_north); southbtn = (Button) Findviewbyid (r.id.btn_ EASTBTN = (Button) Findviewbyid (r.id.btn_east); westbtn = (button) Findviewbyid (r.id.btn_west);p opbtn = (button) Findviewbyid (R.id.btn_pop); Northbtn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {addnorthfragment ();}}); Southbtn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {addsouthfragment ();}}); Eastbtn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {addeastfragment ();}}); Westbtn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {addwestfragment ();}}); Popbtn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) { Fragmentmanager.popbackstack ();}});} private void Addnorthfragment () {addfragment (R.anim.fragment_slide_in_from_top,r.anim.fragment_slide_out_to_top, R.anim.fragment_slide_in_from_top,r.anim.fragment_slide_out_to_top, 0xa0ff0000);} private void Addsouthfragment () {addfragment (r.anim.fragment_slide_in_from_bottom,r.anim.fragment_slide_out_to_ Bottom,r.anim.fragment_slide_in_from_bottom,r.anim.fragment_slide_out_to_bottom, 0xa000ff00);} private void Addeastfragment () {addfragment (R.anim.fragment_slide_in_from_left,r.anim.fragment_slide_out_to_left, R.anim.fragment_slide_in_from_left,r.anim.fragment_slide_out_to_left, 0xa00000ff);} private void Addwestfragment () {addfragment (r.anim.fragment_slide_in_from_right,r.anim.fragment_slide_out_to_ Right,r.anim.fragment_slide_in_from_right,r.anim.fragment_slide_out_to_right, 0xa0ff00ff);} /** * Add the fragment * * @param arg0 * @param arg1 * @param arg2 * @param arg3 * @param color */private void addfragmen T (int arg0, int arg1, int arg2, int arg3, int color) {fragmenttransaction ft = fragmentmanager.begintransaction (); FT.SETCU Stomanimations (arg0, Arg1, arg2, ARG3); Myfragment fragment = new Myfragment (); Bundle bundle = new bundle (); Bundle.putint ("Color", color); fragment.setarguments (bundle); Ft.add (R.id.rl_container, fragment); Ft.addtobackstack (null); Ft.commitallowingstateloss ();} @Overridepublic Boolean ONCREATEOPTIONSMENU (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) { return true;} return super.onoptionsitemselected (item);}}
Very dazzling GIF effect, I will not post, you download try to know.
。
Github:https://github.com/nuptboyzhb/fragmentanimationdemo
Perhaps the problem:
The operation of the animation is asynchronous.
Assuming you want to monitor the operation of the animation, you can rewrite the fragment inside for example the following methods
/** * If you need add animation listener for the fragment * Please use this method */@Overridepublic animation Oncreateani mation (int Transit, boolean enter, int nextanim) {Animation anim;if (enter) {anim = Animationutils.loadanimation (getactiv ity (), Android. r.anim.fade_in);} else {anim = Animationutils.loadanimation (Getactivity (), Android. R.anim.fade_out);} Anim.setanimationlistener (New Animationlistener () {public void Onanimationend (Animation Animation) {}public void Onanimationrepeat (Animation Animation) {}public void Onanimationstart (Animation Animation) {}}); return anim;}
And then in the callback, do what you want to do.
-------------------------------------------------------------------
Many other exchanges, Android Development Alliance QQ Group: 272209595
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android:fragment animated Things