Animation 分類說明
前面分析了Animation基類,然后簡單概述了動畫原理,Animation動畫概述
Animation可以利用類和XML文件兩種方式生成動畫:
下面講解如何使用Animation的子類,包括:
- translate (平移動畫) --- TranslateAnimation
- scale (縮放動畫) --- ScaleAnimation
- rotate (旋轉動畫) --- RotateAnimation類
- alpha ( 透明度動畫) --- AlphaAnimation 類
使用xml動畫文件方式,利用 AnimationUtils loadAnimation()加載xml文件,可以解析生成對應動畫。
更多請參閱官方文檔https://developer.android.google.cn/reference/android/view/animation/Animation
1 TranslateAnimation 平移動畫
構造函數(shù):
//從資源文件加載
TranslateAnimation(Context context, AttributeSet attrs)
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
參數(shù)說明:

參數(shù)說明
fromXDelta:移動的起始點X軸坐標,可以是具體數(shù)值、百分數(shù)、百分數(shù)+p 三種樣式,比如 10、10%、10%p
fromYDelta:移動的 起始點Y軸從標,可以是數(shù)值、百分數(shù)、百分數(shù)p 三種樣式;
toXDelta : 移動的結束點X軸坐標
toYDelta : 移動的結束點Y軸坐標

**fromXType : ** fromXValue的坐標類型
fromXValue: X軸方向移動的初始坐標
toXType: toXValue的坐標類型
toXValue X軸方向結束的坐標
fromYType: fromYValue的坐標類型
fromYValue: Y軸方向移動的起始點坐標
toYType: toYValue的坐標類型
toYValue: Y軸方向結束的坐標
坐標類型Type的種類:
ABSOLUTE,RELATIVE_TO_SELF,RELATIVE_TO_PARENT第一個代表具體值,第二個相對于view自己,第三個相對于父布局。
重要:所有坐標類似(fromXdelta,toXDelta,fromYDelta,ToYDelta)的值都是相對于View的左上角,所以動畫坐標的原點都是View自己的左上角。view的左上角為動畫進行的坐標原點(0,0)
第一個構造函數(shù)和第二個構造函數(shù)的結構不同,且第一個構造函數(shù)沒有坐標類型,但是它的數(shù)值可以分成三類可以是具體數(shù)值、百分數(shù)、百分數(shù)+p 三種樣式這三類就分別對應了ABSOLUTE,RELATIVE_TO_SELF,RELATIVE_TO_PARENT。
第一個構造函數(shù)中的屬性可以用到xml動畫文件中, 第二個則側重代碼生成平移動畫。
XML實現(xiàn)移動動畫
具體值:
一個大小為400,200的view移動400,200的距離,之后保存動畫后的狀態(tài),為了標識view移動了會在view的底部繪制一個大小位置一樣的粉色view作為參照。
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="400"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="3000"
android:fillAfter="true">
</translate>

兩個400*200的TextView相互覆蓋,然后移動頂部的TextView移動(400,200)的距離。fillAfter為true表示動畫結束時保持動畫最終的效果。
如果利用百分數(shù)如何實現(xiàn)上面相同的代碼,百分數(shù)是相對于自身大小,所以直接使用100%即可
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100%"
android:fromYDelta="0"
android:toYDelta="100%"
android:duration="3000"
android:fillAfter="true">
</translate>
100%相當于view的寬和高的100%。

可以看到結果相同。
利用百分數(shù)+p實現(xiàn)從頂部移動parent寬高的一半距離。
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="50%p"
android:fromYDelta="0"
android:toYDelta="50%p"
android:duration="3000"
android:fillAfter="true">
</translate>
結果圖,紅色框是截圖是畫上的,為了標識移動了整個父view的寬高的一半。

如果設置fromXDelta和fromYDelta為負呢?
由于動畫坐標原點都是view的左上角,所以如果為負,動畫開始時會出現(xiàn)在view的左上方。
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%"
android:toXDelta="50%p"
android:fromYDelta="-100%"
android:toYDelta="50%p"
android:duration="3000"
android:fillAfter="true">
</translate>

代碼方式實現(xiàn),簡單例子
TranslateAnimation translateAnimation = new TranslateAnimation(0, 400, 0, 200);
translateAnimation.setDuration(3000);
translateAnimation.setFillAfter(true);
mTVDemo.startAnimation(translateAnimation);
2 AlphaAnimation 透明度動畫
構造函數(shù)
AlphaAnimation(Context context, AttributeSet attrs):讀取xml文件生成
AlphaAnimation(float fromAlpha, float toAlpha):
參數(shù)說明:
fromAlpha:開始的透明度,toAlpha:結束時的透明度
取值: 1.0f代表不透明 , 0.0f表示全透明
xml實現(xiàn)
實現(xiàn)view從透明度0.1,變化到1.0.
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
android:fillBefore="true">
</alpha>

代碼實現(xiàn)
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(3000);
alphaAnimation.setFillBefore(true);
mTVDemo.startAnimation(alphaAnimation);
3 ScaleAnimation 縮放動畫
構造函數(shù):
ScaleAnimation(Context context, AttributeSet attrs):利用xml文件生成對象
ScaleAnimation(float fromX, float toX, float fromY, float toY)
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
參數(shù)說明
fromXScale X軸方向上動畫開始時相對自身的縮放比例,取值float,1.0表示沒有縮放,大于1.0表示放大,小于1.0表示縮小
toXScale X軸方向上動畫結束時相對自身的縮放比例;
fromYScale Y軸方向上動畫開始時相對自身的縮放比例,
toYScale X軸方向上動畫結束時相對自身的縮放比例;
pivotX X軸方向上相對于原點(view左上角)的移動坐標,移動后作為新的縮放原點,可以是數(shù)值、百分數(shù)、百分數(shù)p 三種樣式。
pivotY Y軸方向上相對于原點的移動坐標,意義和android:pivotX一樣。
pivotXType 坐標類型,類似TranslateAnimation的坐標類型,主要用于代碼生成animation時,指定坐標類型
pivotYType 坐標類型,類似TranslateAnimation的坐標類型,主要用于代碼生成animation時,指定坐標類型
變換的坐標原點依然是view的左上角
XML方式實現(xiàn)動畫
從坐標原點(view左上角),view由0,放大兩倍。
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="0.0"
android:toYScale="2.0"
android:pivotX="0"
android:pivotY="0"
android:duration="3000"
android:fillAfter="true"/>
在以view左上角為坐標原點從0經(jīng)歷3秒放大兩倍view。

修改坐標原點為view的中心點
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="0.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fillAfter="true"/>
利用百分比,移動到view的中心進行縮放

利用百分數(shù)+p,設置縮放原點
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="2.0"
android:fromYScale="0.0"
android:toYScale="2.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:duration="3000"
android:fillAfter="true"/>
利用百分數(shù)+p 移動縮放點左右都移動父view的一半,然后進行縮放。

類似投影方式,縮放原點和最終圖片的關系:

代碼實現(xiàn)
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f);
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
mTVDemo.startAnimation(scaleAnimation);
4 RotateAnimation 旋轉動畫
構造函數(shù)
RotateAnimation(Context context, AttributeSet attrs)
RotateAnimation(float fromDegrees, float toDegrees)
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
參數(shù)說明
- fromDegrees 開始旋轉的角度,正值順時針,負值逆時針
- toDegrees 結束時旋轉到的角度,正值順時針,負值逆時針
- pivotX 旋轉起點X軸坐標,可以是數(shù)值、百分數(shù)、百分數(shù)p 和上面的類似規(guī)則一樣,左上角為坐標原點。
- pivotY 旋轉起點Y軸坐標,可以是數(shù)值、百分數(shù)、百分數(shù)p
- pivotXType,pivotYType 類似和其他動畫類含義類似。
XML方式實現(xiàn):
實現(xiàn)view從0度順時針旋轉270度,坐標原點為view左上角(0,0)
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="270"
android:pivotX="0"
android:pivotY="0"
android:duration="3000"
android:fillAfter="true">
</rotate>

利用百分數(shù),修改坐標原點為view的中心點
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fillAfter="true">
</rotate>
繞中心旋轉

代碼實現(xiàn):
RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 270f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
mTVDemo.startAnimation(rotateAnimation);
5 AnimationSet 聯(lián)合動畫
animationSet繼承自Animation沒有自己的屬性完全繼承父類,但是有些屬性對它無效,屬性說明:
- duration, repeatMode, fillBefore, fillAfter: 這些屬性設置給了AnimationSet會作用于它內部的Animation對象.
- repeatCount, fillEnabled: 這些屬性對AnimationSet無效,將被忽略.
- startOffset, shareInterpolator: 這些屬性只作用于AnimationSet.
構造函數(shù)
AnimationSet(Context context, AttributeSet attrs)
AnimationSet(boolean shareInterpolator) shareInterpolator取值為true時,指在AnimationSet中定義一個插值器(interpolater),它下面的所有動畫共同使用,為false,則各自定義插值器。
常用方法:添加動畫:
public void addAnimation (Animation a)
XML方式實現(xiàn)動畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100" />
<alpha
android:fromAlpha="0.5"
android:toAlpha="1.0" />
<scale
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2.0"
android:toYScale="2.0" />
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="-270" />
</set>

代碼實現(xiàn)
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
mTVDemo.startAnimation(animationSet);
Animation動畫概述和執(zhí)行原理
Android動畫之補間動畫TweenAnimation
Android動畫之逐幀動畫FrameAnimation
Android動畫之插值器簡介和系統(tǒng)默認插值器
Android動畫之插值器Interpolator自定義
Android動畫之視圖動畫的缺點和屬性動畫的引入
Android動畫之ValueAnimator用法和自定義估值器
Android動畫之ObjectAnimator實現(xiàn)補間動畫和ObjectAnimator自定義屬性
Android動畫之ObjectAnimator中ofXX函數(shù)全解析-自定義Property,TypeConverter,TypeEvaluator
Android動畫之AnimatorSet聯(lián)合動畫用法
Android動畫之LayoutTransition布局動畫
Android動畫之共享元素動畫
Android動畫之ViewPropertyAnimator(專用于view的屬性動畫)
Android動畫之Activity切換動畫overridePendingTransition實現(xiàn)和Theme Xml方式實現(xiàn)
Android動畫之ActivityOptionsCompat概述
Android動畫之場景變換Transition動畫的使用
Android動畫之Transition和TransitionManager使用
Android動畫之圓形揭露動畫Circular Reveal
Android 動畫之 LayoutAnimation 動畫
Android動畫之視圖動畫的缺點和屬性動畫的引入