Android动画进阶—使用开源动画库nineoldandroids

来源:互联网 发布:数据安全管理方案 编辑:程序博客网 时间:2024/05/16 10:56
http://blog.csdn.net/singwhatiwanna/article/details/17639987

前言

Android系统支持原生动画,这为应用开发者开发绚丽的界面提供了极大的方便,有时候动画是很必要的,当你想做一个滑动的特效的时候,如果苦思冥想都搞不定,那么你可以考虑下动画,说不定动画轻易就搞定了。下面再简单回顾下Android中的动画,本文后面会介绍一个稍微复杂点的动画,先上效果图


动画分类

View动画:也叫渐变动画,针对View的动画,主要支持平移、旋转、缩放、透明度

Drawable动画:也叫帧动画,主要是设置View的背景,可以以动画的形式为View设置多张背景

对象属性动画(Android3.0新加入):可以对对象的属性进行动画而不仅仅是View,动画默认时间间隔300ms,默认帧率10ms/帧。其可以达到的效果是:在一个时间间隔内完成对象从一个属性值到另一个属性值的改变,因此,属性动画几乎是无所不能的,只要对象有这个属性,它都能实现动画效果,但是属性动画从Android3.0才有,这就严重制约了属性动画的使用,这就是开源动画库nineoldandroids的作用,采用nineoldandroids,可以在3.0以前的系统上使用属性动画,nineoldandroids的网址是:http://nineoldandroids.com。说到属性动画,就不得不提到插值器(TimeInterpolator)和估值算法(TypeEvaluator),下面介绍。

TimeInterpolator和TypeEvaluator

TimeInterpolator中文翻译为时间插值器,它的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比,系统预置的有LinearInterpolator(线性插值器:匀速动画)、AccelerateDecelerateInterpolator(加速减速插值器:动画两头慢中间快)和DecelerateInterpolator(减速插值器:动画越来越慢)等;TypeEvaluator的中文翻译为类型估值算法,它的作用是根据当前属性改变的百分比来计算改变后的属性值,系统预置的有IntEvaluator(针对整型属性)、FloatEvaluator(针对浮点型属性)和ArgbEvaluator(针对Color属性)。可能这么说还有点晦涩,没关系,下面给出一个实例就很好理解了。


看上述动画,很显然上述动画是一个匀速动画,其采用了线性插值器和整型估值算法,在40ms内,View的x属性实现从0到40的变换,由于动画的默认刷新率为10ms/帧,所以该动画将分5帧进行,我们来考虑第三帧(x=20 t=20ms),当时间t=20ms的时候,时间流逝的百分比是0.5 (20/40=0.5),意味这现在时间过了一半,那x应该改变多少呢,这个就由插值器和估值算法来确定。拿线性插值器来说,当时间流逝一半的时候,x的变换也应该是一半,即x的改变是0.5,为什么呢?因为它是线性插值器,是实现匀速动画的,下面看它的源码:

[java] view plain copy
  1. public class LinearInterpolator implements Interpolator {  
  2.   
  3.     public LinearInterpolator() {  
  4.     }  
  5.       
  6.     public LinearInterpolator(Context context, AttributeSet attrs) {  
  7.     }  
  8.       
  9.     public float getInterpolation(float input) {  
  10.         return input;  
  11.     }  
  12. }  

很显然,线性插值器的返回值和输入值一样,因此插值器返回的值是0.5,这意味着x的改变是0.5,这个时候插值器的工作就完成了。

具体x变成了什么值,这个需要估值算法来确定,我们来看看整型估值算法的源码:

[java] view plain copy
  1. public class IntEvaluator implements TypeEvaluator<Integer> {  
  2.   
  3.     public Integer evaluate(float fraction, Integer startValue, Integer endValue) {  
  4.         int startInt = startValue;  
  5.         return (int)(startInt + fraction * (endValue - startInt));  
  6.     }  
  7. }  

上述算法很简单,evaluate的三个参数分别表示:估值小数、开始值和结束值,对应于我们的例子就分别是:0.5,0,40。根据上述算法,整型估值返回给我们的结果是20,这就是(x=20 t=20ms)的由来。

说明:属性动画要求该属性有set方法和get方法(可选);插值器和估值算法除了系统提供的外,我们还可以自定义,实现方式也很简单,因为插值器和估值算法都是一个接口,且内部都只有一个方法,我们只要派生一个类实现接口就可以了,然后你就可以做出千奇百怪的动画效果。具体一点就是:自定义插值器需要实现Interpolator或者TimeInterpolator,自定义估值算法需要实现TypeEvaluator。还有就是如果你对其他类型(非int、float、color)做动画,你必须要自定义类型估值算法。

nineoldandroids介绍

其功能和android.animation.*中的类的功能完全一致,使用方法完全一样,只要我们用nineoldandroids来编写动画,就可以在所有的Android系统上运行。比较常用的几个动画类是:ValueAnimator、ObjectAnimator和AnimatorSet,其中ObjectAnimator继承自ValueAnimator,AnimatorSet是动画集,可以定义一组动画。使用起来也是及其简单的,下面举几个小栗子。

栗子1:改变一个对象(myObject)的 translationY属性,让其沿着Y轴向上平移一段距离:它的高度,该动画在默认时间内完成,动画的完成时间是可以定义的,想要更灵活的效果我们还可以定义插值器和估值算法,但是一般来说我们不需要自定义,系统已经预置了一些,能够满足常用的动画。

[java] view plain copy
  1. ObjectAnimator.ofFloat(myObject, "translationY", -myObject.getHeight()).start();  

栗子2:改变一个对象的背景色属性,典型的情形是改变View的背景色,下面的动画可以让背景色在3秒内实现从0xFFFF8080到0xFF8080FF的渐变,并且动画会无限循环而且会有反转的效果

[java] view plain copy
  1. ValueAnimator colorAnim = ObjectAnimator.ofInt(this"backgroundColor"/*Red*/0xFFFF8080/*Blue*/0xFF8080FF);  
  2. colorAnim.setDuration(3000);  
  3. colorAnim.setEvaluator(new ArgbEvaluator());  
  4. colorAnim.setRepeatCount(ValueAnimator.INFINITE);  
  5. colorAnim.setRepeatMode(ValueAnimator.REVERSE);  
  6. colorAnim.start();  

栗子3:动画集合,5秒内对View的旋转、平移、缩放和透明度都进行了改变

[java] view plain copy
  1. AnimatorSet set = new AnimatorSet();  
  2. set.playTogether(  
  3.     ObjectAnimator.ofFloat(myView, "rotationX"0360),  
  4.     ObjectAnimator.ofFloat(myView, "rotationY"0180),  
  5.     ObjectAnimator.ofFloat(myView, "rotation"0, -90),  
  6.     ObjectAnimator.ofFloat(myView, "translationX"090),  
  7.     ObjectAnimator.ofFloat(myView, "translationY"090),  
  8.     ObjectAnimator.ofFloat(myView, "scaleX"11.5f),  
  9.     ObjectAnimator.ofFloat(myView, "scaleY"10.5f),  
  10.     ObjectAnimator.ofFloat(myView, "alpha"10.25f, 1)  
  11. );  
  12. set.setDuration(5 * 1000).start();  

栗子4:下面是个简单的调用方式,其animate方法是nineoldandroids特有的

[java] view plain copy
  1. Button myButton = (Button)findViewById(R.id.myButton);  
  2.   
  3. //Note: in order to use the ViewPropertyAnimator like this add the following import:  
  4. //  import static com.nineoldandroids.view.ViewPropertyAnimator.animate;  
  5. animate(myButton).setDuration(2000).rotationYBy(720).x(100).y(100);  

栗子5:一个采用nineoldandroids实现的稍微复杂点的动画

布局xml如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/menu"  
  8.         style="@style/MenuStyle"  
  9.         android:background="@drawable/menu" />  
  10.   
  11.     <Button  
  12.         android:id="@+id/item1"  
  13.         style="@style/MenuItemStyle"  
  14.         android:background="@drawable/circle1"  
  15.         android:visibility="gone" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/item2"  
  19.         style="@style/MenuItemStyle"  
  20.         android:background="@drawable/circle2"  
  21.         android:visibility="gone" />  
  22.   
  23.     <Button  
  24.         android:id="@+id/item3"  
  25.         style="@style/MenuItemStyle"  
  26.         android:background="@drawable/circle3"  
  27.         android:visibility="gone" />  
  28.   
  29.     <Button  
  30.         android:id="@+id/item4"  
  31.         style="@style/MenuItemStyle"  
  32.         android:background="@drawable/circle4"  
  33.         android:visibility="gone" />  
  34.   
  35.     <Button  
  36.         android:id="@+id/item5"  
  37.         style="@style/MenuItemStyle"  
  38.         android:background="@drawable/circle5"  
  39.         android:visibility="gone" />  
  40. </FrameLayout>  

代码如下:

[java] view plain copy
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.   
  3.     private static final String TAG = "MainActivity";  
  4.   
  5.     private Button mMenuButton;  
  6.     private Button mItemButton1;  
  7.     private Button mItemButton2;  
  8.     private Button mItemButton3;  
  9.     private Button mItemButton4;  
  10.     private Button mItemButton5;  
  11.   
  12.     private boolean mIsMenuOpen = false;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         initView();  
  20.     }  
  21.   
  22.     @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  23.     private void initView() {  
  24.         mMenuButton = (Button) findViewById(R.id.menu);  
  25.         mMenuButton.setOnClickListener(this);  
  26.   
  27.         mItemButton1 = (Button) findViewById(R.id.item1);  
  28.         mItemButton1.setOnClickListener(this);  
  29.   
  30.         mItemButton2 = (Button) findViewById(R.id.item2);  
  31.         mItemButton2.setOnClickListener(this);  
  32.   
  33.         mItemButton3 = (Button) findViewById(R.id.item3);  
  34.         mItemButton3.setOnClickListener(this);  
  35.   
  36.         mItemButton4 = (Button) findViewById(R.id.item4);  
  37.         mItemButton4.setOnClickListener(this);  
  38.   
  39.         mItemButton5 = (Button) findViewById(R.id.item5);  
  40.         mItemButton5.setOnClickListener(this);  
  41.     }  
  42.   
  43.     @Override  
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         mMenuButton.performClick();  
  46.         getMenuInflater().inflate(R.menu.main, menu);  
  47.         return false;  
  48.     }  
  49.   
  50.     @Override  
  51.     public void onClick(View v) {  
  52.         if (v == mMenuButton) {  
  53.             if (!mIsMenuOpen) {  
  54.                 mIsMenuOpen = true;  
  55.                 doAnimateOpen(mItemButton1, 05300);  
  56.                 doAnimateOpen(mItemButton2, 15300);  
  57.                 doAnimateOpen(mItemButton3, 25300);  
  58.                 doAnimateOpen(mItemButton4, 35300);  
  59.                 doAnimateOpen(mItemButton5, 45300);  
  60.             } else {  
  61.                 mIsMenuOpen = false;  
  62.                 doAnimateClose(mItemButton1, 05300);  
  63.                 doAnimateClose(mItemButton2, 15300);  
  64.                 doAnimateClose(mItemButton3, 25300);  
  65.                 doAnimateClose(mItemButton4, 35300);  
  66.                 doAnimateClose(mItemButton5, 45300);  
  67.             }  
  68.         } else {  
  69.             Toast.makeText(this"你点击了" + v, Toast.LENGTH_SHORT).show();  
  70.         }  
  71.   
  72.     }  
  73.   
  74.     /** 
  75.      * 打开菜单的动画 
  76.      * @param view 执行动画的view 
  77.      * @param index view在动画序列中的顺序 
  78.      * @param total 动画序列的个数 
  79.      * @param radius 动画半径 
  80.      */  
  81.     private void doAnimateOpen(View view, int index, int total, int radius) {  
  82.         if (view.getVisibility() != View.VISIBLE) {  
  83.             view.setVisibility(View.VISIBLE);  
  84.         }  
  85.         double degree = Math.PI * index / ((total - 1) * 2);  
  86.         int translationX = (int) (radius * Math.cos(degree));  
  87.         int translationY = (int) (radius * Math.sin(degree));  
  88.         Log.d(TAG, String.format("degree=%f, translationX=%d, translationY=%d",  
  89.                 degree, translationX, translationY));  
  90.         AnimatorSet set = new AnimatorSet();  
  91.         //包含平移、缩放和透明度动画  
  92.         set.playTogether(  
  93.                 ObjectAnimator.ofFloat(view, "translationX"0, translationX),  
  94.                 ObjectAnimator.ofFloat(view, "translationY"0, translationY),  
  95.                 ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f),  
  96.                 ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f),  
  97.                 ObjectAnimator.ofFloat(view, "alpha", 0f, 1));  
  98.         //动画周期为500ms  
  99.         set.setDuration(1 * 500).start();  
  100.     }  
  101.   
  102.     /** 
  103.      * 关闭菜单的动画 
  104.      * @param view 执行动画的view 
  105.      * @param index view在动画序列中的顺序 
  106.      * @param total 动画序列的个数 
  107.      * @param radius 动画半径 
  108.      */  
  109.     private void doAnimateClose(final View view, int index, int total,  
  110.             int radius) {  
  111.         if (view.getVisibility() != View.VISIBLE) {  
  112.             view.setVisibility(View.VISIBLE);  
  113.         }  
  114.         double degree = Math.PI * index / ((total - 1) * 2);  
  115.         int translationX = (int) (radius * Math.cos(degree));  
  116.         int translationY = (int) (radius * Math.sin(degree));  
  117.         Log.d(TAG, String.format("degree=%f, translationX=%d, translationY=%d",  
  118.                 degree, translationX, translationY));  
  119.         AnimatorSet set = new AnimatorSet();  
  120.       //包含平移、缩放和透明度动画  
  121.         set.playTogether(  
  122.                 ObjectAnimator.ofFloat(view, "translationX", translationX, 0),  
  123.                 ObjectAnimator.ofFloat(view, "translationY", translationY, 0),  
  124.                 ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f),  
  125.                 ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f),  
  126.                 ObjectAnimator.ofFloat(view, "alpha", 1f, 0f));  
  127.         //为动画加上事件监听,当动画结束的时候,我们把当前view隐藏  
  128.         set.addListener(new AnimatorListener() {  
  129.             @Override  
  130.             public void onAnimationStart(Animator animator) {  
  131.             }  
  132.   
  133.             @Override  
  134.             public void onAnimationRepeat(Animator animator) {  
  135.             }  
  136.   
  137.             @Override  
  138.             public void onAnimationEnd(Animator animator) {  
  139.                 view.setVisibility(View.GONE);  
  140.             }  
  141.   
  142.             @Override  
  143.             public void onAnimationCancel(Animator animator) {  
  144.             }  
  145.         });  
  146.   
  147.         set.setDuration(1 * 500).start();  
  148.     }  
  149. }  
代码下载:http://download.csdn.net/detail/singwhatiwanna/6782865
0 0
原创粉丝点击