【android】Fragment动画那点事

来源:互联网 发布:经传软件账号密码 编辑:程序博客网 时间:2024/05/17 06:53

最近很多人对Fragment的动画很感兴趣,我就做个例子给大家看看。既然要做,我就做下面几类动画:

弹入弹出动画:从“上下左右”进入,从“上下左右”弹出,当然,你怎么组合都可以。另外你也可以加一些透明度的变化,这就看你的发挥了。。。

1.先写动画的xml文件

做开发的都知道,在/res/anim/目录下,新建xml的动画文件,比如:

fragment_slide_in_from_bottom.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="800"  
  4.     android:fromYDelta="100.0%p"  
  5.     android:interpolator="@android:anim/decelerate_interpolator"  
  6.     android:toYDelta="0.0" />  

fragment_slide_in_from_left.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="800"  
  4.     android:fromXDelta="-100.0%p"  
  5.     android:interpolator="@android:anim/decelerate_interpolator"  
  6.     android:toXDelta="0.0" />  

fragment_slide_in_from_right.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="800"  
  4.     android:fromXDelta="100.0%p"  
  5.     android:interpolator="@android:anim/decelerate_interpolator"  
  6.     android:toXDelta="0.0" />  

fragment_slide_in_from_top.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="800"  
  4.     android:fromYDelta="-100.0%p"  
  5.     android:interpolator="@android:anim/decelerate_interpolator"  
  6.     android:toYDelta="0.0" />  

上面的是进入动画,至于弹出动画,只需要将from和to的值翻转一下即可。你们都懂得,不懂得,直接去github上clone,地址在下面。

2.添加Fragment的时候,使用setCustomAnimations方法。

直接贴代码,简单明了。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.testfragment;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentManager;  
  5. import android.support.v4.app.FragmentTransaction;  
  6. import android.support.v7.app.ActionBarActivity;  
  7. import android.view.Menu;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. /** 
  13.  *  
  14.  * @author Zheng Haibo 
  15.  * @web  http://www.mobctrl.net 
  16.  * 
  17.  */  
  18. public class MainActivity extends ActionBarActivity {  
  19.   
  20.     private FragmentManager fragmentManager;  
  21.   
  22.     private Button northBtn;  
  23.     private Button southBtn;  
  24.     private Button eastBtn;  
  25.     private Button westBtn;  
  26.     private Button popBtn;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.         fragmentManager = getSupportFragmentManager();  
  33.         initButton();  
  34.     }  
  35.   
  36.     private void initButton() {  
  37.         northBtn = (Button) findViewById(R.id.btn_north);  
  38.         southBtn = (Button) findViewById(R.id.btn_south);  
  39.         eastBtn = (Button) findViewById(R.id.btn_east);  
  40.         westBtn = (Button) findViewById(R.id.btn_west);  
  41.         popBtn = (Button) findViewById(R.id.btn_pop);  
  42.         northBtn.setOnClickListener(new OnClickListener() {  
  43.   
  44.             @Override  
  45.             public void onClick(View arg0) {  
  46.                 addNorthFragment();  
  47.             }  
  48.         });  
  49.         southBtn.setOnClickListener(new OnClickListener() {  
  50.   
  51.             @Override  
  52.             public void onClick(View arg0) {  
  53.                 addSouthFragment();  
  54.             }  
  55.         });  
  56.         eastBtn.setOnClickListener(new OnClickListener() {  
  57.   
  58.             @Override  
  59.             public void onClick(View arg0) {  
  60.                 addEastFragment();  
  61.             }  
  62.         });  
  63.         westBtn.setOnClickListener(new OnClickListener() {  
  64.   
  65.             @Override  
  66.             public void onClick(View arg0) {  
  67.                 addWestFragment();  
  68.             }  
  69.         });  
  70.   
  71.         popBtn.setOnClickListener(new OnClickListener() {  
  72.   
  73.             @Override  
  74.             public void onClick(View arg0) {  
  75.                 fragmentManager.popBackStack();  
  76.             }  
  77.         });  
  78.   
  79.     }  
  80.   
  81.     private void addNorthFragment() {  
  82.         addFragment(R.anim.fragment_slide_in_from_top,  
  83.                 R.anim.fragment_slide_out_to_top,  
  84.                 R.anim.fragment_slide_in_from_top,  
  85.                 R.anim.fragment_slide_out_to_top, 0xa0ff0000);  
  86.     }  
  87.   
  88.     private void addSouthFragment() {  
  89.         addFragment(R.anim.fragment_slide_in_from_bottom,  
  90.                 R.anim.fragment_slide_out_to_bottom,  
  91.                 R.anim.fragment_slide_in_from_bottom,  
  92.                 R.anim.fragment_slide_out_to_bottom, 0xa000ff00);  
  93.     }  
  94.   
  95.     private void addEastFragment() {  
  96.         addFragment(R.anim.fragment_slide_in_from_left,  
  97.                 R.anim.fragment_slide_out_to_left,  
  98.                 R.anim.fragment_slide_in_from_left,  
  99.                 R.anim.fragment_slide_out_to_left, 0xa00000ff);  
  100.     }  
  101.   
  102.     private void addWestFragment() {  
  103.         addFragment(R.anim.fragment_slide_in_from_right,  
  104.                 R.anim.fragment_slide_out_to_right,  
  105.                 R.anim.fragment_slide_in_from_right,  
  106.                 R.anim.fragment_slide_out_to_right, 0xa0ff00ff);  
  107.     }  
  108.   
  109.     /** 
  110.      * add the fragment 
  111.      *  
  112.      * @param arg0 
  113.      * @param arg1 
  114.      * @param arg2 
  115.      * @param arg3 
  116.      * @param color 
  117.      */  
  118.     private void addFragment(int arg0, int arg1, int arg2, int arg3, int color) {  
  119.         FragmentTransaction ft = fragmentManager.beginTransaction();  
  120.         ft.setCustomAnimations(arg0, arg1, arg2, arg3);  
  121.         MyFragment fragment = new MyFragment();  
  122.         Bundle bundle = new Bundle();  
  123.         bundle.putInt("color", color);  
  124.         fragment.setArguments(bundle);  
  125.         ft.add(R.id.rl_container, fragment);  
  126.         ft.addToBackStack(null);  
  127.         ft.commitAllowingStateLoss();  
  128.     }  
  129.   
  130.     @Override  
  131.     public boolean onCreateOptionsMenu(Menu menu) {  
  132.         getMenuInflater().inflate(R.menu.main, menu);  
  133.         return true;  
  134.     }  
  135.   
  136.     @Override  
  137.     public boolean onOptionsItemSelected(MenuItem item) {  
  138.         int id = item.getItemId();  
  139.         if (id == R.id.action_settings) {  
  140.             return true;  
  141.         }  
  142.         return super.onOptionsItemSelected(item);  
  143.     }  
  144. }  

很炫的GIF效果,我就不贴了,你下载试试就知道了。。

Github: https://github.com/nuptboyzhb/FragmentAnimationDemo

后续问题:

animation的执行是异步的。如果你想对animation的执行进行监听,你可以重写fragment里面的如下方法

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * if you need add animation listener for the fragment 
  3.      * please use this method 
  4.      */  
  5.     @Override  
  6.     public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {  
  7.         Animation anim;  
  8.         if (enter) {  
  9.             anim = AnimationUtils.loadAnimation(getActivity(),  
  10.                     android.R.anim.fade_in);  
  11.         } else {  
  12.             anim = AnimationUtils.loadAnimation(getActivity(),  
  13.                     android.R.anim.fade_out);  
  14.         }  
  15.   
  16.         anim.setAnimationListener(new AnimationListener() {  
  17.             public void onAnimationEnd(Animation animation) {  
  18.                   
  19.             }  
  20.   
  21.             public void onAnimationRepeat(Animation animation) {  
  22.                   
  23.             }  
  24.   
  25.             public void onAnimationStart(Animation animation) {  
  26.                   
  27.             }  
  28.         });  
  29.   
  30.         return anim;  
  31.     }  

然后在回调里,做你想做的事

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

更多交流,Android开发联盟QQ群:272209595



转自:http://blog.csdn.net/nupt123456789/article/details/40185105

0 0
原创粉丝点击