Android?補(bǔ)間動(dòng)畫及組合AnimationSet常用方法詳解
補(bǔ)間動(dòng)畫
Android常用的四種補(bǔ)間動(dòng)畫分別為RotateAnimation、ScaleAnimation、TranslateAnimation、AlphaAnimation,他們的父類為Animation,UML類圖如下:

父類通用方法有:
- public void setFillBefore(boolean fillBefore): 動(dòng)畫完成后,View是否會(huì)停留在動(dòng)畫開始的狀態(tài),默認(rèn)為true
- public void setFillAfter(boolean fillAfter) :動(dòng)畫完成后,View是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài),優(yōu)先級(jí)大于fillBefore,默認(rèn)為false
- public void setFillEnabled(boolean fillEnabled) :控制setFillBefore的值是否生效,如果fillEnabled為false,則setFillBefore無論為何值都不會(huì)再生效。
- public boolean isFillEnabled():返回
setFillEnabled設(shè)置的值。 - public void setStartOffset(long startOffset):設(shè)置動(dòng)畫延遲啟動(dòng)時(shí)間,單位是ms
- public void setDuration(long durationMillis):設(shè)置動(dòng)畫持續(xù)時(shí)間,如果設(shè)置
durationMillis < 0會(huì)拋異常。 - public void setRepeatMode(int repeatMode):
RESTART:正序重新開始、REVERSE:倒序重新開始,默認(rèn)是RESTART。注意:repeatCount(count)設(shè)置的count值必須>0或者是INFINITE才會(huì)生效 - public void setRepeatCount(int repeatCount):設(shè)置動(dòng)畫應(yīng)該重復(fù)的次數(shù)。如果為
0,則動(dòng)畫只播放一次;如果設(shè)置為N(N>0),則將繼續(xù)播放N次;如果設(shè)置為INFINITE,則將無限輪播。默認(rèn)為0。 - public void setInterpolator(Interpolator i):設(shè)置差值器,影響動(dòng)畫的加速曲線。默認(rèn)為
AccelerateDecelerateInterpolator。 - public void setAnimationListener(AnimationListener listener) : 設(shè)置動(dòng)畫監(jiān)聽,
AnimationListener中的幾個(gè)方法如下:
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationEnd(animation: Animation?) {
//動(dòng)畫結(jié)束時(shí)回調(diào),注意:當(dāng)repeatCount設(shè)置為Animation.INFINITE不再收到該回調(diào)
log("onAnimationEnd")
}
override fun onAnimationStart(animation: Animation?) {
//動(dòng)畫開始時(shí)回調(diào)
log("onAnimationStart")
}
override fun onAnimationRepeat(animation: Animation?) {
//repeatCount設(shè)置為Animation.INFINITE時(shí)動(dòng)畫每執(zhí)行一次該方法回調(diào)就會(huì)執(zhí)行一次
log("onAnimationRepeat")
}
})
RotateAnimation
旋轉(zhuǎn)動(dòng)畫,通過設(shè)置目標(biāo)View的旋轉(zhuǎn)中心、旋轉(zhuǎn)角度來達(dá)到旋轉(zhuǎn)目的。RotateAnimation中需要特殊設(shè)置的幾個(gè)參數(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:動(dòng)畫開始之前的旋轉(zhuǎn)偏移量
- toDegrees:動(dòng)畫結(jié)束時(shí)的旋轉(zhuǎn)偏移量
- pivotXType:用于設(shè)置旋轉(zhuǎn)中心
X軸計(jì)量類型,可以設(shè)置成Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT之一,pivotXType與pivotXValue組合之后可以表示不同的X軸坐標(biāo)點(diǎn)。
Animation.ABSOLUTE:目標(biāo)View的X軸坐標(biāo) = View左上角的原點(diǎn) + pivotXValue數(shù)值的點(diǎn)(y方向同理)
Animation.RELATIVE_TO_SELF:目標(biāo)View的X軸坐標(biāo) = View左上角的原點(diǎn) + View自身寬度 * pivotXValue數(shù)值
Animation.RELATIVE_TO_PARENT:目標(biāo)View的X軸坐標(biāo) = View左上角的原點(diǎn) + View父控件寬度 * pivotXValue
- pivotXValue:旋轉(zhuǎn)
View所圍繞的點(diǎn)的X坐標(biāo)。如果pivotXType為absolute,此值可以是一個(gè)絕對(duì)數(shù)字,否則可以是一個(gè)0.1f~1.0f 的值。 - pivotYType:同
pivotXType,表示的的是y軸 - pivotYValue:同
pivotXValue,表示的的是y軸
動(dòng)畫示例
方式一:代碼動(dòng)態(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時(shí)注意:pivotX pivotY,可取值為數(shù)字,百分比,或者百分比p
- 設(shè)置為數(shù)字時(shí),表示px像素值。如設(shè)置為
10,那么中心坐標(biāo)為View的左上角的原點(diǎn)在x方向和y方向加上10px的點(diǎn)。對(duì)應(yīng)代碼中的Animation.ABSOLUTE單位。 - 設(shè)置為百分比時(shí)(如50%),中心坐標(biāo)為
View的左上角的原點(diǎn)在x方向加上自身寬度50%和y方向自身高度50%的點(diǎn)(即自身中間的位置)。對(duì)應(yīng)代碼中的Animation.RELATIVE_TO_SELF。 - 設(shè)置為百分比
p時(shí)(如50%p),中心坐標(biāo)為View的左上角的原點(diǎn)在x方向加上父控件寬度50%和y方向父控件高度50%的點(diǎn)。對(duì)應(yīng)代碼中的Animation.RELATIVE_TO_PARENT
ScaleAnimation
縮放動(dòng)畫,設(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軸方向在動(dòng)畫開始時(shí)、結(jié)束時(shí)對(duì)應(yīng)的縮放因子 - fromY / toY:分別表示
Y軸方向在動(dòng)畫開始時(shí)、結(jié)束時(shí)對(duì)應(yīng)的縮放因子 pivotXType、pivotXValue、pivotYType、pivotYValue具體含義在上面的RotateAnimation已經(jīng)講到,這四個(gè)值在ScaleAnimation主要是用來確定縮放中心的。
動(dòng)畫示例
方式一:代碼動(dòng)態(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
平移動(dòng)畫,是指對(duì)目標(biāo)View進(jìn)行平移操作。
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è)不同的構(gòu)造方法:
- 第一種把類型固定為
ABSOLUTE,那么傳入的float fromXDelta, float toXDelta, float fromYDelta, float toYDelta都為固定數(shù)值,即(fromXDelta, fromYDelta)、(toXDelta、toYDelta)分別代表起始坐標(biāo)與結(jié)束坐標(biāo)。 - 第二種傳入的類型可以為
Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT的一種,那么對(duì)應(yīng)的fromXValue、toXValue、fromYValue、toYValue可以是具體數(shù)值或或者0.0~1.0(表示目標(biāo)View本身或著父View的百分比)。
動(dòng)畫示例
方式一:代碼動(dòng)態(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
透明度動(dòng)畫
public AlphaAnimation(float fromAlpha, float toAlpha) {
mFromAlpha = fromAlpha;
mToAlpha = toAlpha;
}
- fromAlpha: 動(dòng)畫開始時(shí)的透明度: 1.0表示完全不透明,0.0表示完全透明。
- toAlpha: 動(dòng)畫結(jié)束時(shí)透明度: 1.0表示完全不透明,0.0表示完全透明。
動(dòng)畫示例
方式一:代碼動(dòng)態(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 動(dòng)畫組合
動(dòng)畫組合AnimationSet,本身也繼承自Animation,可以將多個(gè)動(dòng)畫組合起來使用。如下屬性在AnimationSet中設(shè)置時(shí)的注意事項(xiàng):
- duration, repeatMode, fillBefore, fillAfter: 當(dāng)在
AnimationSet對(duì)象上設(shè)置這些屬性時(shí),將被下推到所有子動(dòng)畫。 - repeatCount, fillEnabled: 這些屬性在
AnimationSet中被忽略。 - startOffset, shareInterpolator: 這些屬性應(yīng)用于
AnimationSet本身。
動(dòng)畫示例
方式1:代碼動(dòng)態(tài)生成
/**
* 動(dòng)畫組合AnimationSet
* - duration, repeatMode, fillBefore, fillAfter:當(dāng)在AnimationSet對(duì)象上設(shè)置這些屬性時(shí),將被下推到所有子動(dòng)畫。
* - repeatCount, fillEnabled:這些屬性在AnimationSet中被忽略。
* - startOffset, shareInterpolator: 這些屬性應(yīng)用于AnimationSet本身
*/
private fun loadAnimSet(): Animation {
//方式1:代碼動(dòng)態(tài)生成
val rotateAnim = loadRotationAnim()
val alphaAnim = loadAlphaAnimation()
val translateAnim = loadTranslateAnimation()
val scaleAnim = loadScaleAnimation()
/**
* shareInterpolator: 如果想讓集合中的所有動(dòng)畫都使用與AnimationSet中
* 設(shè)置的一樣的插值器,則傳true;反之,如果集合中每個(gè)動(dòng)畫都使用自己的插值器,則傳false.
*/
val animSet = AnimationSet(true).apply {
duration = 3000
interpolator = LinearInterpolator()
fillAfter = true
repeatMode = Animation.REVERSE
startOffset = 100 //延遲執(zhí)行動(dòng)畫,應(yīng)用于AnimationSet本身
}
//animSet.cancel() //取消動(dòng)畫
//animSet.reset() //重置動(dòng)畫
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í)行動(dòng)畫-->
<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 補(bǔ)間動(dòng)畫及組合AnimationSet常用方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Android 補(bǔ)間動(dòng)畫組合AnimationSet的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法總結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)異步消息處理機(jī)制的幾種方法,結(jié)合實(shí)例形式詳細(xì)總結(jié)分析了Android異步消息處理機(jī)制的原理、相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
Android應(yīng)用中使用ListView來分頁顯示刷新的內(nèi)容
這篇文章主要介紹了Android應(yīng)用中使用ListView來分頁顯示刷新的內(nèi)容的方法,展示了一個(gè)點(diǎn)擊按鈕進(jìn)行刷新的實(shí)例以及下拉刷新分頁顯示的要點(diǎn)解析,需要的朋友可以參考下2016-04-04
Android11文件管理權(quán)限申請(qǐng)?jiān)敿?xì)介紹
大家好,本篇文章主要講的是Android11文件管理權(quán)限申請(qǐng)?jiān)敿?xì)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android編程判斷手機(jī)上是否安裝了某個(gè)程序的方法
這篇文章主要介紹了Android編程判斷手機(jī)上是否安裝了某個(gè)程序的方法,涉及Android針對(duì)程序包的操作及進(jìn)程判斷的相關(guān)技巧,需要的朋友可以參考下2015-11-11
淺談AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)的分析說明
本篇文章是對(duì)AnDroidDraw+DroidDraw實(shí)現(xiàn)Android程序UI設(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
android:TextView簡(jiǎn)單設(shè)置文本樣式和超鏈接的方法
這篇文章主要介紹了android:TextView簡(jiǎn)單設(shè)置文本樣式和超鏈接的方法,涉及TextView常見文字屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android Tablayout 自定義Tab布局的使用案例
這篇文章主要介紹了Android Tablayout 自定義Tab布局的使用案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
這篇文章主要介紹了Android實(shí)現(xiàn)的秒表計(jì)時(shí)器,結(jié)合完整實(shí)例形式分析了Android計(jì)時(shí)器的具體實(shí)現(xiàn)步驟與相關(guān)技巧,涉及Android針對(duì)日期與時(shí)間的操作方法,需要的朋友可以參考下2016-08-08

