本篇主要爲Android Animation分析
導讀:
1.Animation是什麼?
2.Animation分類
3.Tween Animation
4.監聽器和插值器
5. LayoutAnimationsController
6.Frame-By-Frame Animation
7.Property Animation
8.總結
一. Animation是什麼?
Animation在英文裏是動畫的意思,在Android中,使用Animation實現Android UI界面動畫效果,Animations提供了一系列的動畫效果,可以進行旋轉、縮放、淡入淡出等,這些效果可以應用在絕大多數的控件中。
二.Animation的分類
在Android 3.0(API Level 11)以前,Android僅支持2種動畫:分別是Frame-By-Frame Animation(逐幀動畫)和 Tween Animation(補間動畫),在3.0之後Android添加了一種新的動畫系統,稱爲:Property Animation(屬性動畫)。
其中前兩種統稱爲視圖動畫(View Animation)。
三.Tween Animation(補間動畫)
概念:補間動畫指我們只需指定開始、結束時的“關鍵幀“,變化中的其他幀由系統來計算,從而達到動畫效果。
補間動畫的優點是可以節省空間。目前Tween Animation支持的動畫效果有以下5種。
AlphaAnimation(漸變)
Translate Animation(位移)
Scale Animation(縮放)
Rotate Animation(旋轉)
Animation Set(動畫集合)
Tween Animation第一種使用方法:
1.創建一個animationset對象,
2.根據需要創建相應的animation對象
3.爲animation設置相應的數據
4.將animation對象添加到animationset對象當中去,
5.設置空間執行animationset
首先在佈局裏放置啓動動畫按鈕和動畫演示圖片,在java代碼中設置相應的監聽器之後,在監聽器裏添加動畫代碼
//Alpha動畫
/*第一步:創建animationset對象
*第二步:創建alphaanimation對象,設置相關參數
*第三步:設置時長(毫秒)
* 第四步:添加動畫
* 第五步:設置動畫
*/
AnimationSet animationset=new AnimationSet(true);
//參數說明:0,1分別代表着起始透明度和終止透明度
AlphaAnimation alphaanimation=new AlphaAnimation(0,1);
alphaanimation.setDuration(1000);
animationset.addAnimation(alphaanimation);
image.startAnimation(animationset);
// Translate動畫
//參數裏的Animation.RELATIVE_TO_SELF是指相對於自身,另外還有相對父佈局,絕對位置兩種參數
AnimationSet animationset=new AnimationSet(true);
TranslateAnimation ta=new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f, //相對參考 x起始
Animation.RELATIVE_TO_SELF,2.5f, //相對參考 x結束
Animation.RELATIVE_TO_SELF,0f, // 相對參考 y開始
Animation.RELATIVE_TO_SELF,-2.5f //相對參考 y結束
);
ta.setDuration(1000);
animationset.addAnimation(ta);
image.startAnimation(animationset);
// Rotate動畫
AnimationSet animationset = new AnimationSet(true);
//參數一:從?角度開始旋轉
//參數二:旋轉到?角度
//參數三:依靠的x軸是父佈局還是自己
//參數四:相對參數三旋轉偏移量
//參數五:依靠的y軸是父佈局還是自己
//參數六:相對參數五旋轉偏移量
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT,
0.2f, Animation.RELATIVE_TO_SELF, -0.2f);
ra.setDuration(12000);
animationset.addAnimation(ra);
image.startAnimation(animationset);
//Scale動畫
AnimationSet animationset=new AnimationSet(true);
//參數1,2:x軸從?縮小到?
//參數3,4:y軸從?縮小到?
//參數5,6:相對x軸參考及縮放量
//參數7,8:相對y軸參考及縮放量
ScaleAnimation sa=new ScaleAnimation(1,0.5f,1,0.5f
,Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF
,1.0f);
sa.setDuration(1000);
animationset.addAnimation(sa);
image.startAnimation(animationset);
以上是補間的四種基本動畫,另外還有AnimationSet集合動畫
首先什麼是AnimationSet?
AnimationSet是Animation的子類
一個AnimationSet包含一系列的Animation對象;怎麼使用AnimationSet?
- AnimationSet 使用方法同其他動畫類似,代碼如下
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alpha = new AlphaAnimation(0.1f,1.0f);
//一個漸變動畫對象
RotateAnimation rotateAnimation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//一個旋轉動畫對象
RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT,
0.2f, Animation.RELATIVE_TO_SELF, -0.2f);
//第二個旋轉動畫對象
animationSet.addAnimation(alpha);
//添加第一個漸變對象
animationSet.addAnimation(rotateAnimation);
//添加第一個旋轉對象
animationSet.addAnimation(ra);
//添加第二個旋轉對象
animationSet.setDuration(2000);
//設置間隔時長
animationSet.setStartOffset(500);
//設置動畫開始前等待
image.startAnimation(animationSet);
//設置動畫
Tween Animation第二種使用方法:
使用第一種方法代碼複用性不高,所以有了第二種:在xml中製作動畫,在java中使用
具體方法:
1.在res下面新建anim文件夾
2.創建xml文件,首先加入< set >標籤
3,在標籤內加入四種動畫標籤(四選一或者set集合)
例:
單個動畫
< alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
>
集合動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="2000"
/>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
</set>
4,在代碼裏設置動畫
//參數一:context
//參數二:xml文件
Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);
imageView.startAnimation(animation);
補充在Activity跳轉時的動畫配置方式
startActivity(intent);
overridePendingTransition(R.anim.rotate,R.anim.rotate); //設置傳入動畫,傳出動畫
Tweened Animations通用屬性:
1.setDuration(1000) 設置持續時間
2.setFillAfter(boolean) true時,動畫結束保留狀態
3.setFillBefore(boolean) true時,動畫結束回到初始
4.setStartOffSet(long) 動畫開始前等待時間
5.setRepeatCount(int) 動畫重複次數
在實際項目中,我們經常使用補間動畫,原因是補間動畫使用起來比較方便,功能也比逐幀動畫強大不少,而且還可以很方便地進行動畫疊加,實現更加複雜的效果,但是由於其交互性差,逐漸被屬性動畫所取代。
四.監聽器和插值器
AnimationListener(動畫監聽器)是一個監聽器,該監聽器在動畫執行的各個階段會得到通知,從而調用相應的方法;AnimationListener主要包括如下三個方法:onAnimationEnd(Animation animation) - 當動畫結束時調用onAnimationRepeat(Animation animation) - 當動畫重複時調用
onAniamtionStart(Animation animation) - 當動畫啓動時調用
監聽器使用方法:
alpha.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//添加邏輯
}
@Override
public void onAnimationEnd(Animation animation) {
//添加邏輯
}
@Override
public void onAnimationRepeat(Animation animation) {
//添加邏輯
}
});
通過這個監聽回調,可以獲取到動畫的開始,結束和重複事件,並針對相應的事件作出不同的處理。
Interpolator(插值器)定義了動畫變化的速率,在Animations框架當中定義了以下四種情況。
1. AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候速率快。
2.AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始加速
3. CycleInterpolator:動畫循環播放特定的次數,速率改變沿着正弦曲線
4 .DecelerateInterpolator:在動畫開始的地方速率改變比較慢,然後開始減速
5. LinearInterpolator:動畫以均勻的速率改變
插值器使用方法一:
在xml裏使用,這種方式很簡單,只需要在set標籤裏設置就可以
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
>
android:shareInterpolator=”true” 代表插值器共享給每一個動畫
如果 android:shareInterpolator=”false” 那麼需要爲每一個動畫單獨設置插值器
在動畫標籤裏添加插值器 android:interpolator=”@android:anim/accelerate_interpolator”
插值器使用方法二:
首先我們要明白下面第一行代碼(這個是在java使用動畫的第一行代碼)裏的參數的意思
AnimationSet animationSet=new AnimationSet(true);
//true是共享interpoltor的意思(所有對象共享)
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
//設置插值器
所有對象共享插值器,指代set集合裏如果有很多種基礎動畫,那麼他們將採用同一個速率規則來執行。如果這個參數爲false,那麼我們可以爲動畫本身設置插值方案。
alpha.setInterpolator(new AccelerateDecelerateInterpolator());
//設置alpha插值器
translate.setInterpolator(new AccelerateDecelerateInterpolator());
//設置 translate插值器
未完待續
本文參考:《Android羣英傳》 ,Mars Android視頻第二季
如有錯誤,請在留言區指出。