Android:fragment animation that little thing

Source: Internet
Author: User

Recently a lot of people are interested in fragment animation, I will give you an example to see. Now that I'm going to do it, I'll do the following types of animations:

Pop-up animation: from "Up and down" into, from "up and down" pop up, of course, how you can combine. 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, in the/res/anim/directory, the new XML animation files, such as:

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 "/>

The above is to enter the animation, as for the pop-up animation, only need to flip from and to the value of a bit. You all know, do not know, go directly to GitHub clone, the address is below.

2. When adding fragment, use the Setcustomanimations method.

Direct 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

-------------------------------------------------------------------

more communication, Android Development Alliance QQ Group: 272209595



Android:fragment animation that little thing

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.