View動畫效果:
1.>>Tween動畫
通過對View的內容進行一系列的圖形變換(平移、縮放、旋轉、透明度變換)實現動畫效果,補間動畫需要使用<set>節點作為根節點,子節點里可以為下表格中的四種動畫標簽,也可以包繼續含<set>標簽;動畫的定義xml文件需要添加到res/anim文件夾中;
動畫類型 | Xml定義動畫使用的節點 | 編碼定義動畫使用的類 |
漸變透明度動畫效果 | <alpha/> | AlphaAnimation |
漸變尺寸縮放動畫效果 | <scale/> | ScaleAnimation |
畫面位置移動動畫效果 | <translate/> | TranslateAnimation |
畫面旋轉動畫效果 | <rotate/> | RotateAnimation |
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" > <alpha android:duration="5000" android:fromAlpha="1.0"<!-- 1代表可見,0代表不可見 --> android:toAlpha="0" > <!-- 持續時間duration --> </alpha> <rotate android:duration="5000" android:fromDegrees="0" android:pivotX="50%" // 旋轉參考點 android:pivotY="50%" // 旋轉參考點 android:toDegrees="180" > </rotate> <scale android:duration="5000" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="5" android:toYScale="5" /> <!-- 動畫開始時的比例大小,結束時的比例,縮放參考點 --> <translate android:duration="5000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="50" android:toYDelta="50" /> <!-- 從(0,0)到(50,50) --> </set>
在java代碼中設置開始動畫:
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);// 使用rotate.xml生成動畫效果對象 animation.setFillAfter(true);// 動畫停止時保持在該動畫結束時的狀態
ImageView.startAnimation(animation);
Animation animation1 = new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 編碼方式
2.>>Frame動畫,即順序播放事先做好的圖像,開發步驟:
(1)把定義好的圖片放進項目res/drawable下;
(2)在項目的res目錄下創建anim文件夾,在此文件夾下定義動畫xml文件,每一項靜態的圖像添加到drawable目錄中,或者用編碼方式定義動畫效果;
(3)為View控件綁定動畫效果,調用代表動畫的AnimationDrawable的start()方法開始動畫;
TextView tv = (TextView) findViewById(R.id.tv); tv.setBackgroundResource(R.drawable.frame); // 綁定frame動畫,會發送一個消息到主線程的消息隊列處理器等待處理,綁定事件完成之后才能進行動畫啟動 final AnimationDrawable drawable = (AnimationDrawable) findViewById(R.id.tv).getBackground(); // Looper().myQueue():取得消息隊列,要消息(事件)處理完成之后才執行addIdleHandler添加進的handler中的方法 Looper.myQueue().addIdleHandler( new MessageQueue.IdleHandler() { @Override public boolean queueIdle() { drawable.start();// 啟動動畫,要綁定事件處理完成之后才能啟動動畫 return false;// 只要執行操作之后就會從消息隊列中移出 } });// 取得處理主線程中處理的消息隊列
注意: Frame動畫必須使用<animation-list>作為根節點,下面是frame.xml文件的定義;
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" ><!-- 動畫只播放一次 --> <item android:drawable="@drawable/a1" android:duration="500"></item> <!--第一項為一個靜態的圖像--> <item android:drawable="@drawable/a2" android:duration="500"></item> <item android:drawable="@drawable/a3" android:duration="500"></item> </animation-list>
3.>>屬性動畫
屬性動畫可以使對象的屬性值在一定時間間隔內變化到某一個值,如在1000毫秒內移動控件的位置(改變x,y的值),在0.5秒內改變alpha屬性的值以改變控件透明度,屬性動畫資源文件位於res/animator目錄中;如下是一個屬性動畫的定義
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially" > // 動畫執行順序,默認是同時執行,在此設置為按順序執行 <set> // 定義x,y的值在0.5秒內移動到(400,300) 在此未設置android:ordering屬性,此set則為同時執行 <objectAnimator android:duration="500" android:propertyName="x" android:valueTo="400" android:valueType="intType" /> <objectAnimator android:duration="500" android:propertyName="y" android:valueTo="300" android:valueType="intType" /> </set> <objectAnimator android:duration="500" android:propertyName="alpha" android:valueTo="1f" /> </set>
加載屬性動畫資源文件:
// 加載動畫資源 AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.aim); // 設置要控制的對象 set.setTarget(null); set.start();
4.>>LayoutAnimaionController
LayoutAnimaionController為Layout或者viewGroup里的控件設置動畫效果,特點是它會使其中的每個控件都有相同的動畫效果,這些控件的動畫效果可以在不同的時間顯示出來。
mLayout = (LinearLayout) findViewById(R.id.ll); Animation anim = new TranslateAnimation(0, 200, 0, 0); anim.setDuration(500); anim.setFillAfter(true); LayoutAnimationController layoutAnim = new LayoutAnimationController(anim); // 設置動畫 layoutAnim.setOrder(LayoutAnimationController.ORDER_NORMAL); // 設置子View動畫順序 mLayout.setLayoutAnimation(layoutAnim);
關於LayoutAnimaionController,它需要一個Animation對象用於實例化,可以設置子View動畫順序,有三種順序方式
LayoutAnimaionController .ORDER_NORMAL // 順序
LayoutAnimaionController .ORDER_REVERSE //反序
LayoutAnimaionController .ORDER_RANDOM //隨機
還可以設置延遲setDelay(param);
5.>>Interpolator動畫速率
/** * An interpolator defines the rate of change of an animation. This allows * the basic animation effects (alpha, scale, translate, rotate) to be * accelerated, decelerated, repeated, etc. */ public interface Interpolator extends TimeInterpolator { }
查看接口Interpolator的定義,Interpolator接口定義動畫改變的速度,如基本的動畫效果(透明度、比例、移動、旋轉)的加速、減速、重復等;
/** * TimeInterpolator定義動畫速率的改變,允許動畫有非固定的運動如加速、減速 */ public interface TimeInterpolator { /** * @param input 一個介於0到1的參數標識當前點的位置,0代表開始,1代表結束 * @return 返回一個動畫補插值.這個值可以小於1{在目標的后面}或大於1{在目標的后面} */ float getInterpolation(float input); }
Android已經定義了幾個該接口的直接子類,在程序中可以直接調用
——AccelerateInterpolator:動畫從開始到結束,變化率是一個加速的過程。
——DecelerateInterpolator:動畫從開始到結束,變化率是一個減速的過程。
——CycleInterpolator:動畫從開始到結束,變化率是循環給定次數的正弦曲線。
——Anticipate/Overshoot:往起點方向偏移少量距離再開始加速動畫/往終點方向偏移少量距離再結束動畫。
——Bounce:彈性效果。
——AccelerateDecelerateInterpolator:動畫從開始到結束,變化率是先加速后減速的過程。
——LinearInterpolator:動畫從開始到結束,變化率是線性變化。
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" > <!--表明在下面的動畫中分享Interpolator--> <scale android:duration="100" android:fillAfter="false" android:fromXScale="0.5" android:fromYScale="0.5"android:interpolator="@android:anim/accelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:startOffset="-50" android:toXScale="1.2" android:toYScale="1.2" /> </set>
上面是使用系統自帶的Interpolator的方法,也可以自定義Interpolator
public class MyInter implements Interpolator { // ... @Override public float getInterpolation(float input) { // ... return 0; } // ... }
在使用的時候,使用如下的方式;
ImageView iv = new ImageView(getContext()); Animation animation = AnimationUtils.loadAnimation(getContext(), 0x70982743); animation.setInterpolator(new MyInter()); iv.startAnimation(animation);
Activity切換動畫效果:
關於Activity切換動畫效果,網上比較普遍的是overridePendingTransition(enterAnim, exitAnim),但這種方式其實有一些問題;例如:被打開的Activity退出時,並沒有動畫效果;並且,如果需要當前Activity銷毀返回到前一個Activity時,當前Activity和前一個Activity都執行動畫,這種方法就根本不能滿足了。
言歸正傳,直接上我淘到的方法:通過Theme對Activity動畫效果進行設置
<style name="AppTheme" parent="@android:style/Theme"> <!-- 設置activity切換動畫 --> <item name="android:windowAnimationStyle">@style/activityAnimation</item> </style> <!-- animation 樣式 --> <style name="activityAnimation" parent="@android:style/Animation"> <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item> <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item> <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item> <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item> </style>
上面activityAnimation下面四個item對應的動畫分別為(假設從Activity A 進入到Activity B):
進入B時B執行的動畫;
進入B時A執行的動畫;
離開B返回A時A執行的動畫;
離開B返回A時B執行的動畫;
兩個google官方參考鏈接:
http://developer.android.com/reference/android/R.attr.html
http://developer.android.com/reference/android/R.styleable.html#WindowAnimation