Android?補間動畫及組合AnimationSet常用方法詳解
補間動畫
Android
常用的四種補間動畫分別為RotateAnimation
、ScaleAnimation
、TranslateAnimation
、AlphaAnimation
,他們的父類為Animation
,UML
類圖如下:
父類通用方法有:
- public void setFillBefore(boolean fillBefore): 動畫完成后,View是否會停留在動畫開始的狀態(tài),默認為true
- public void setFillAfter(boolean fillAfter) :動畫完成后,View是否會停留在動畫結(jié)束的狀態(tài),優(yōu)先級大于fillBefore,默認為false
- public void setFillEnabled(boolean fillEnabled) :控制setFillBefore的值是否生效,如果fillEnabled為false,則setFillBefore無論為何值都不會再生效。
- public boolean isFillEnabled():返回
setFillEnabled
設(shè)置的值。 - public void setStartOffset(long startOffset):設(shè)置動畫延遲啟動時間,單位是ms
- public void setDuration(long durationMillis):設(shè)置動畫持續(xù)時間,如果設(shè)置
durationMillis < 0
會拋異常。 - public void setRepeatMode(int repeatMode):
RESTART
:正序重新開始、REVERSE
:倒序重新開始,默認是RESTART
。注意:repeatCount(count)
設(shè)置的count
值必須>0
或者是INFINITE
才會生效 - public void setRepeatCount(int repeatCount):設(shè)置動畫應該重復的次數(shù)。如果為
0
,則動畫只播放一次;如果設(shè)置為N(N>0)
,則將繼續(xù)播放N
次;如果設(shè)置為INFINITE
,則將無限輪播。默認為0
。 - public void setInterpolator(Interpolator i):設(shè)置差值器,影響動畫的加速曲線。默認為
AccelerateDecelerateInterpolator
。 - public void setAnimationListener(AnimationListener listener) : 設(shè)置動畫監(jiān)聽,
AnimationListener
中的幾個方法如下:
animation.setAnimationListener(object : Animation.AnimationListener { override fun onAnimationEnd(animation: Animation?) { //動畫結(jié)束時回調(diào),注意:當repeatCount設(shè)置為Animation.INFINITE不再收到該回調(diào) log("onAnimationEnd") } override fun onAnimationStart(animation: Animation?) { //動畫開始時回調(diào) log("onAnimationStart") } override fun onAnimationRepeat(animation: Animation?) { //repeatCount設(shè)置為Animation.INFINITE時動畫每執(zhí)行一次該方法回調(diào)就會執(zhí)行一次 log("onAnimationRepeat") } })
RotateAnimation
旋轉(zhuǎn)動畫,通過設(shè)置目標View
的旋轉(zhuǎn)中心、旋轉(zhuǎn)角度來達到旋轉(zhuǎn)目的。RotateAnimation
中需要特殊設(shè)置的幾個參數(shù)如下:
// RotateAnimation.java public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mPivotXValue = pivotXValue; mPivotXType = pivotXType; mPivotYValue = pivotYValue; mPivotYType = pivotYType; initializePivotPoint(); }
- fromDegrees:動畫開始之前的旋轉(zhuǎn)偏移量
- toDegrees:動畫結(jié)束時的旋轉(zhuǎn)偏移量
- pivotXType:用于設(shè)置旋轉(zhuǎn)中心
X軸
計量類型,可以設(shè)置成Animation.ABSOLUTE
、Animation.RELATIVE_TO_SELF
、Animation.RELATIVE_TO_PARENT
之一,pivotXType
與pivotXValue
組合之后可以表示不同的X軸
坐標點。
Animation.ABSOLUTE:目標View的X軸坐標 = View左上角的原點 + pivotXValue數(shù)值的點(y方向同理)
Animation.RELATIVE_TO_SELF:目標View的X軸坐標 = View左上角的原點 + View自身寬度 * pivotXValue數(shù)值
Animation.RELATIVE_TO_PARENT:目標View的X軸坐標 = View左上角的原點 + View父控件寬度 * pivotXValue
- pivotXValue:旋轉(zhuǎn)
View
所圍繞的點的X坐標
。如果pivotXType
為absolute
,此值可以是一個絕對數(shù)字,否則可以是一個0.1f~1.0f 的值。 - pivotYType:同
pivotXType
,表示的的是y軸 - pivotYValue:同
pivotXValue
,表示的的是y軸
動畫示例
方式一:代碼動態(tài)創(chuàng)建
val rotateAnim: Animation = RotateAnimation( 0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f ) rotateAnim.duration = 2000 rotateAnim.repeatCount = Animation.INFINITE rotateAnim.interpolator = LinearInterpolator() rotateAnim.fillAfter = true mTvTarget.animation = rotateAnim
方式二:XML
中設(shè)置
//view_rotate.xml中: <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:repeatMode="restart" android:toDegrees="360" />
代碼中引用:
mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_rotate))
執(zhí)行效果:
在XML中設(shè)置pivotX、pivotY時注意:pivotX pivotY,可取值為數(shù)字,百分比,或者百分比p
- 設(shè)置為數(shù)字時,表示px像素值。如設(shè)置為
10
,那么中心坐標為View
的左上角的原點在x方向
和y方向
加上10px
的點。對應代碼中的Animation.ABSOLUTE
單位。 - 設(shè)置為百分比時(如50%),中心坐標為
View
的左上角的原點在x方向
加上自身寬度50%和y方向
自身高度50%的點(即自身中間的位置)。對應代碼中的Animation.RELATIVE_TO_SELF
。 - 設(shè)置為百分比
p
時(如50%p
),中心坐標為View
的左上角的原點在x方向
加上父控件寬度50%和y方向
父控件高度50%的點。對應代碼中的Animation.RELATIVE_TO_PARENT
ScaleAnimation
縮放動畫,設(shè)置的參數(shù)如下:
public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) { mResources = null; mFromX = fromX; mToX = toX; mFromY = fromY; mToY = toY; mPivotXValue = pivotXValue; mPivotXType = pivotXType; mPivotYValue = pivotYValue; mPivotYType = pivotYType; initializePivotPoint(); }
- fromX / toX:分別表示
X軸
方向在動畫開始時、結(jié)束時對應的縮放因子 - fromY / toY:分別表示
Y軸
方向在動畫開始時、結(jié)束時對應的縮放因子 pivotXType、pivotXValue、pivotYType、pivotYValue
具體含義在上面的RotateAnimation
已經(jīng)講到,這四個值在ScaleAnimation
主要是用來確定縮放中心的。
動畫示例
方式一:代碼動態(tài)創(chuàng)建
val scaleAnim = ScaleAnimation( 1.0f, 0.5f, 1.0f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f).apply { duration = 1000 repeatCount = Animation.INFINITE repeatMode = Animation.REVERSE fillAfter = true } mTvTarget.animation = scaleAnim
方式二:XML
中創(chuàng)建
// view_scale.xml <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fillAfter="true" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:repeatMode="reverse" android:toXScale="0.5" android:toYScale="0.5" />
代碼中引入:
mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_scale))
執(zhí)行結(jié)果:
TranslateAnimation
平移動畫,是指對目標View
進行平移操作。
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) { mFromXValue = fromXDelta; mToXValue = toXDelta; mFromYValue = fromYDelta; mToYValue = toYDelta; mFromXType = ABSOLUTE; mToXType = ABSOLUTE; mFromYType = ABSOLUTE; mToYType = ABSOLUTE; } public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) { mFromXValue = fromXValue; mToXValue = toXValue; mFromYValue = fromYValue; mToYValue = toYValue; mFromXType = fromXType; mToXType = toXType; mFromYType = fromYType; mToYType = toYType; }
可以看到有兩個不同的構(gòu)造方法:
- 第一種把類型固定為
ABSOLUTE
,那么傳入的float fromXDelta, float toXDelta, float fromYDelta, float toYDelta
都為固定數(shù)值,即(fromXDelta, fromYDelta)
、(toXDelta、toYDelta)
分別代表起始坐標與結(jié)束坐標。 - 第二種傳入的類型可以為
Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT
的一種,那么對應的fromXValue、toXValue、fromYValue、toYValue
可以是具體數(shù)值或或者0.0~1.0
(表示目標View
本身或著父View
的百分比)。
動畫示例
方式一:代碼動態(tài)創(chuàng)建
val translateAnim = TranslateAnimation( Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_SELF, 0.5f) .apply { duration = 2000 fillAfter = false interpolator = LinearInterpolator() repeatMode = Animation.REVERSE repeatCount = Animation.INFINITE } mTvTarget.animation = translateAnim
方式二:XML中創(chuàng)建
// view_translate.xml <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/linear_interpolator" android:repeatCount="infinite" android:repeatMode="reverse" android:toXDelta="50%" android:toYDelta="50%" />
代碼中引入:
mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_translate))
執(zhí)行效果:
AlphaAnimation
透明度動畫
public AlphaAnimation(float fromAlpha, float toAlpha) { mFromAlpha = fromAlpha; mToAlpha = toAlpha; }
- fromAlpha: 動畫開始時的透明度: 1.0表示完全不透明,0.0表示完全透明。
- toAlpha: 動畫結(jié)束時透明度: 1.0表示完全不透明,0.0表示完全透明。
動畫示例
方式一:代碼動態(tài)創(chuàng)建
val alphaAnim = AlphaAnimation(1.0f, 0.2f).apply { duration = 1000 fillAfter = true interpolator = LinearInterpolator() repeatMode = Animation.REVERSE repeatCount = Animation.INFINITE } mTvTarget.animation = alphaAnim
方式二:XML中創(chuàng)建
//view_alpha.xml <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fillAfter="true" android:fromAlpha="1.0" android:interpolator="@android:anim/linear_interpolator" android:repeatCount="infinite" android:repeatMode="reverse" android:toAlpha="0.0" />
代碼中引入:
mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_alpha))
執(zhí)行效果:
AnimationSet 動畫組合
動畫組合AnimationSet
,本身也繼承自Animation
,可以將多個動畫組合起來使用。如下屬性在AnimationSet
中設(shè)置時的注意事項:
- duration, repeatMode, fillBefore, fillAfter: 當在
AnimationSet
對象上設(shè)置這些屬性時,將被下推到所有子動畫。 - repeatCount, fillEnabled: 這些屬性在
AnimationSet
中被忽略。 - startOffset, shareInterpolator: 這些屬性應用于
AnimationSet
本身。
動畫示例
方式1:代碼動態(tài)生成
/** * 動畫組合AnimationSet * - duration, repeatMode, fillBefore, fillAfter:當在AnimationSet對象上設(shè)置這些屬性時,將被下推到所有子動畫。 * - repeatCount, fillEnabled:這些屬性在AnimationSet中被忽略。 * - startOffset, shareInterpolator: 這些屬性應用于AnimationSet本身 */ private fun loadAnimSet(): Animation { //方式1:代碼動態(tài)生成 val rotateAnim = loadRotationAnim() val alphaAnim = loadAlphaAnimation() val translateAnim = loadTranslateAnimation() val scaleAnim = loadScaleAnimation() /** * shareInterpolator: 如果想讓集合中的所有動畫都使用與AnimationSet中 * 設(shè)置的一樣的插值器,則傳true;反之,如果集合中每個動畫都使用自己的插值器,則傳false. */ val animSet = AnimationSet(true).apply { duration = 3000 interpolator = LinearInterpolator() fillAfter = true repeatMode = Animation.REVERSE startOffset = 100 //延遲執(zhí)行動畫,應用于AnimationSet本身 } //animSet.cancel() //取消動畫 //animSet.reset() //重置動畫 animSet.addAnimation(rotateAnim) animSet.addAnimation(alphaAnim) animSet.addAnimation(translateAnim) animSet.addAnimation(scaleAnim) return animSet } mTvTarget.startAnimation(loadAnimSet())
方式2:通過XML
創(chuàng)建:
//view_animation_set.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fillAfter="true" android:fillBefore="false" android:interpolator="@android:anim/linear_interpolator" android:repeatMode="reverse" android:shareInterpolator="true"> <rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:toDegrees="360" /> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:toXScale="0.5" android:toYScale="0.5" /> <!-- startOffset:延遲執(zhí)行動畫--> <set android:fillAfter="true" android:interpolator="@android:anim/linear_interpolator" android:repeatMode="reverse" android:shareInterpolator="true" android:startOffset="100"> <alpha android:fromAlpha="1.0" android:repeatCount="infinite" android:toAlpha="0.0" /> <translate android:fromXDelta="0" android:fromYDelta="0" android:repeatCount="infinite" android:toXDelta="50%" android:toYDelta="50%" /> </set> </set>
代碼中引入:
mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_animation_set))
執(zhí)行結(jié)果:
以上就是Android 補間動畫及組合AnimationSet常用方法詳解的詳細內(nèi)容,更多關(guān)于Android 補間動畫組合AnimationSet的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程實現(xiàn)異步消息處理機制的幾種方法總結(jié)
這篇文章主要介紹了Android編程實現(xiàn)異步消息處理機制的幾種方法,結(jié)合實例形式詳細總結(jié)分析了Android異步消息處理機制的原理、相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2018-08-08Android應用中使用ListView來分頁顯示刷新的內(nèi)容
這篇文章主要介紹了Android應用中使用ListView來分頁顯示刷新的內(nèi)容的方法,展示了一個點擊按鈕進行刷新的實例以及下拉刷新分頁顯示的要點解析,需要的朋友可以參考下2016-04-04淺談AnDroidDraw+DroidDraw實現(xiàn)Android程序UI設(shè)計的分析說明
本篇文章是對AnDroidDraw+DroidDraw實現(xiàn)Android程序UI設(shè)計進行了詳細的分析介紹,需要的朋友參考下2013-05-05android:TextView簡單設(shè)置文本樣式和超鏈接的方法
這篇文章主要介紹了android:TextView簡單設(shè)置文本樣式和超鏈接的方法,涉及TextView常見文字屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08Android Tablayout 自定義Tab布局的使用案例
這篇文章主要介紹了Android Tablayout 自定義Tab布局的使用案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08