1 AnimatorSet概述
已經(jīng)講完了ObjectAnimator的關(guān)鍵用法杰标,可以利用現(xiàn)有的函數(shù)實(shí)現(xiàn)一個動畫改變多個屬性,多個動畫一起執(zhí)行類似ofPropertyValuesHolder和ObjectAnimator的ofxx函數(shù)同時寫入兩個屬性的函數(shù)怀樟,但這些都是在一個動畫中,所有的動畫設(shè)置都是共用的吗货。有時也需要能夠讓多個動畫相互配合的運(yùn)行它碎,AnimatorSet就可以實(shí)現(xiàn)這個功能(其實(shí)我們也可以為每個動畫設(shè)置監(jiān)聽,然后執(zhí)行下一個動畫棚菊,但是操作比較繁瑣)浸踩,AnimatorSet可以實(shí)現(xiàn)復(fù)雜的組合動畫,既可以同時對多個對象做動畫并控制執(zhí)行順序统求,也可以控制單個對象的動畫執(zhí)行順序检碗。
Android Developer :https://developer.android.google.cn/reference/android/animation/AnimatorSet
AnimatorSet可以指定一組動畫的執(zhí)行順序据块,讓它們可以一起執(zhí)行,順序執(zhí)行折剃,延遲執(zhí)行另假。
1.1 AnimatorSet的生成:
代碼直接利用 AnimatorSet()
如果是XML定義AnimatorSet 利用 AnimatorInflater.loadAnimator()加載(使用較少)
AnimatorSet可以作用于ObjectAnimator和ValueAnimator,但通常ObjectAnimator用的比較多怕犁。
1.2 AnimatorSet和ObjectAnimator中重疊的函數(shù)边篮,誰生效?
查看AnimatorSet的方法奏甫,發(fā)現(xiàn)了幾個和Animator動畫相同的設(shè)置屬性的方法戈轿,那么這些方法和內(nèi)部單個動畫同時設(shè)置誰生效呢?
setDuration (long duration):設(shè)置每個內(nèi)部子動畫的時長阵子,默認(rèn)每個子動畫使用自己默認(rèn)的時長思杯,如果AnimatorSet設(shè)置了時長,子動畫將繼承這個時長挠进,而子動畫自己設(shè)置的duration將失效色乾。
下面會列舉其他方法,和setDuration方法類似领突,都是如果沒有設(shè)置就使用子動畫自己的暖璧,如果AnimatorSet設(shè)置了就使用AnimatorSet設(shè)置的屬性。
setInterpolator (TimeInterpolator interpolator)君旦,設(shè)置之后內(nèi)部子動畫的插值器都是這個
setTarget(Object target)咙轩,設(shè)置之后所有內(nèi)部子動畫都作用于相同的target目標(biāo)對象
特例:
setStartDelay(long startDelay)函數(shù)是個特例秧饮,它不會覆蓋子動畫開始延遲芒涡,只對AnimatorSet的開始時間起作用吁峻,所以它會延后AnimatorSet激活整體動畫的開始時間。
比較簡單就不用代碼說明了
1.3 兩種添加動畫到AnimatorSet的方法
- 一種是利用playTogether()或者playSequentially()捞魁,可以一次添加所有的動畫到AnimatorSet中至会,
- 一種是利用play(Animator)構(gòu)建Builder對象,然后利用Builder提供的方法谱俭,一個一個的添加動畫奉件,并設(shè)置各個動畫間執(zhí)行順序的依賴關(guān)系
1.4 AnimatorSet的其他函數(shù)
cancle()取消動畫,會取消AnimatorSet中的所有子動畫昆著。
end() 結(jié)束動畫县貌,會結(jié)束AnimatorSet中的所有子動畫。
getChildAnimations() 獲取所有受AnimatorSet控制的動畫
isStarted(),AnimationSet動畫是否開始了凑懂,true開始
isRunning(),AnimationSet開始之后(isStarted = true)煤痕,內(nèi)部是否有子動畫正在執(zhí)行,有動畫執(zhí)行返回true
pause()暫停動畫,
resume()恢復(fù)暫停的動畫
play(Animator anim)獲取Builder對象
2 playTogether和playSequentially
2.1 playTogether
多個動畫同時執(zhí)行摆碉,可以是對一個對象執(zhí)行的多個動畫塘匣,也可以是對多個對象的多個動畫。
playTogether(Collection<Animator> items) //利用集合添加動畫
playTogether(Animator... items) //利用可變參數(shù)添加動畫
多個動畫作用于一個對象
ObjectAnimator objectAnimator1 = ObjectAnimator.ofArgb(mTextView, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mTextView, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mTextView, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mTextView, "translationY", 0, 250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4);
animatorSet.setDuration(3000);
animatorSet.start();
如果多個動畫同時對一個屬性進(jìn)行操作巷帝,會發(fā)生錯誤嗎忌卤?
不會發(fā)生錯誤,會正常執(zhí)行楞泼,只不過會按照playTogether添加的最后一個動畫覆蓋前面操作相同屬性的動畫驰徊,也可能沒有覆蓋,但確實(shí)是最后一個添加的動畫起了作用堕阔。
ObjectAnimator objectAnimator1 = ObjectAnimator.ofArgb(mTextView, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mTextView, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mTextView, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mTextView, "translationY", 0, 250);
ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mTextView, "translationY", 200, 450);
ObjectAnimator objectAnimator6 = ObjectAnimator.ofFloat(mTextView, "translationY", 300, 600);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4,objectAnimator5,objectAnimator6);
animatorSet.setDuration(3000);
animatorSet.start();
animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4,objectAnimator5,objectAnimator6);
最終會按照objectAnimator6的效果進(jìn)行展示棍厂。
animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator6,objectAnimator5,objectAnimator4);
最終會按照objectAnimator4的效果進(jìn)行展示。
多個動畫作用于多個對象
ObjectAnimator objectAnimator1 = ObjectAnimator.ofArgb(mTextView, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mTextView, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mTextView, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mTextView, "translationY", 0, 250);
ObjectAnimator objectAnimator7 = ObjectAnimator.ofArgb(mTextView2, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator8 = ObjectAnimator.ofFloat(mTextView2, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator9 = ObjectAnimator.ofFloat(mTextView2, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator10 = ObjectAnimator.ofFloat(mTextView2, "translationY", 0, 250);
ObjectAnimator objectAnimator11 = ObjectAnimator.ofArgb(mTextView3, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator12 = ObjectAnimator.ofFloat(mTextView3, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator13 = ObjectAnimator.ofFloat(mTextView3, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator14 = ObjectAnimator.ofFloat(mTextView3, "translationY", 0, 250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4
,objectAnimator7,objectAnimator8,objectAnimator9,objectAnimator10,objectAnimator11,objectAnimator12,objectAnimator13,objectAnimator14);
animatorSet.setDuration(3000);
animatorSet.start();
2.2 playSequentially
順序播放動畫:
playSequentially(List<Animator> items)
playSequentially(Animator... items)
對于playSequentially作用于單個和多個對象的區(qū)別和playTogether一樣印蔬,所以這里只講解playSequentially作用于多個對象勋桶。
上面的代碼修改為playSequentially,并且把時間修改為1000脱衙,否則每個動畫順序執(zhí)行3s會很耗時間侥猬。
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4
,objectAnimator7,objectAnimator8,objectAnimator9,objectAnimator10,objectAnimator11,objectAnimator12,objectAnimator13,objectAnimator14);
animatorSet.setDuration(1000);
animatorSet.start();
注意:
- playTogether和playSequentially只負(fù)責(zé)激活控件動畫,后續(xù)的動畫還是由ObjectAnimator自己控制捐韩。
- playSequentially是一個動畫執(zhí)行完后執(zhí)行下一個動畫退唠,但如果前一個動畫是無限循環(huán),下一個動畫永遠(yuǎn)不會執(zhí)行荤胁。
如何實(shí)現(xiàn)動畫的循環(huán)播放
AnimatorSet中沒有設(shè)置動畫執(zhí)行次數(shù)的函數(shù)瞧预,所以無法利用AnimatorSet設(shè)置動畫循環(huán)播放, playSequentially一個動畫執(zhí)行完后執(zhí)行另外一個動畫仅政,如果前一個動畫是無限循環(huán)垢油,后一個動畫不會執(zhí)行,所以也無法實(shí)現(xiàn)循環(huán)播放動畫圆丹,所以只能利用playTogether播放動畫時每個動畫設(shè)置無線循環(huán):
ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mTextView, "translationY", 200, 450);
ObjectAnimator objectAnimator6 = ObjectAnimator.ofFloat(mTextView, "translationY", 300, 600);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofArgb(mTextView, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mTextView, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mTextView, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mTextView, "translationY", 0, 250);
ObjectAnimator objectAnimator7 = ObjectAnimator.ofArgb(mTextView2, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator8 = ObjectAnimator.ofFloat(mTextView2, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator9 = ObjectAnimator.ofFloat(mTextView2, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator10 = ObjectAnimator.ofFloat(mTextView2, "translationY", 0, 250);
ObjectAnimator objectAnimator11 = ObjectAnimator.ofArgb(mTextView3, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator12 = ObjectAnimator.ofFloat(mTextView3, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator13 = ObjectAnimator.ofFloat(mTextView3, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator14 = ObjectAnimator.ofFloat(mTextView3, "translationY", 0, 250);
objectAnimator1.setRepeatCount(-1);
objectAnimator2.setRepeatCount(-1);
objectAnimator3.setRepeatCount(-1);
objectAnimator4.setRepeatCount(-1);
objectAnimator5.setRepeatCount(-1);
objectAnimator6.setRepeatCount(-1);
objectAnimator7.setRepeatCount(-1);
objectAnimator8.setRepeatCount(-1);
objectAnimator9.setRepeatCount(-1);
objectAnimator10.setRepeatCount(-1);
objectAnimator11.setRepeatCount(-1);
objectAnimator12.setRepeatCount(-1);
objectAnimator13.setRepeatCount(-1);
objectAnimator14.setRepeatCount(-1);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4
,objectAnimator7,objectAnimator8,objectAnimator9,objectAnimator10,objectAnimator11,objectAnimator12,objectAnimator13,objectAnimator14);
// animatorSet.play()
animatorSet.setDuration(3000);
animatorSet.start();
如果利用playSequentially并且前一個動畫是循環(huán)動畫:
所以最終實(shí)現(xiàn)循環(huán)動畫的方法為滩愁,每個內(nèi)部子動畫設(shè)置為無限循環(huán)
3 利用play(Animator)構(gòu)建Builder對象
Builder play(Animator anim);生成builder對象辫封,Builder能夠控制動畫的執(zhí)行順序和相互之間的依賴硝枉。
通過AnimatorSet中的play方法可以獲取AnimatorSet.Builder對象,通過Builder內(nèi)部的函數(shù)添加動畫到AnimatorSet中倦微,后續(xù)的所有動畫都是以play傳入的動畫為標(biāo)準(zhǔn)進(jìn)行相應(yīng)操作(后面會詳細(xì)講述)妻味。
Builder的函數(shù):
public Builder with(Animator anim) 和前面動畫一起執(zhí)行
public Builder before(Animator anim) 執(zhí)行前面的動畫后再執(zhí)行該動畫
public Builder after(Animator anim) 先執(zhí)行這個動畫再執(zhí)行前面動畫
public Builder after(long delay) 延遲n毫秒之后執(zhí)行動畫
public Builder with(Animator anim) 和前面動畫一起執(zhí)行:
例子還用上面的動畫,三個view同時執(zhí)行變色動畫:
ObjectAnimator objectAnimator1 = ObjectAnimator.ofArgb(mTextView, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mTextView, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mTextView, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(mTextView, "translationY", 0, 250);
ObjectAnimator objectAnimator7 = ObjectAnimator.ofArgb(mTextView2, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator8 = ObjectAnimator.ofFloat(mTextView2, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator9 = ObjectAnimator.ofFloat(mTextView2, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator10 = ObjectAnimator.ofFloat(mTextView2, "translationY", 0, 250);
ObjectAnimator objectAnimator11 = ObjectAnimator.ofArgb(mTextView3, "backgroundColor", Color.WHITE, Color.GREEN);
ObjectAnimator objectAnimator12 = ObjectAnimator.ofFloat(mTextView3, "scaleX", 0.1f, 1.2f);
ObjectAnimator objectAnimator13 = ObjectAnimator.ofFloat(mTextView3, "scaleY", 0.5f, 1.0f);
ObjectAnimator objectAnimator14 = ObjectAnimator.ofFloat(mTextView3, "translationY", 0, 250);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).with(objectAnimator7).with(objectAnimator11);
animatorSet.setDuration(3000);
animatorSet.start();
動畫objectAnimator1作用于第一個view欣福,objectAnimator7作用于第二個view责球,objectAnimator11作用于第三個view。
public Builder before(Animator anim) 執(zhí)行前面的動畫后再執(zhí)行該動畫:
第一個view先執(zhí)行變色操作再執(zhí)行X軸縮放操作:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).before(objectAnimator2);
animatorSet.setDuration(3000);
animatorSet.start();
public Builder after(Animator anim) 先執(zhí)行這個動畫再執(zhí)行前面動畫:
第一個view先執(zhí)行X軸縮放動畫,再執(zhí)行變色操作雏逾。
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).after(objectAnimator2);
animatorSet.setDuration(3000);
animatorSet.start();
public Builder after(long delay) 延遲n毫秒之后執(zhí)行動畫
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).after(objectAnimator2).after(10000);
animatorSet.setDuration(3000);
animatorSet.start();
如果設(shè)置過after延遲執(zhí)行之后設(shè)置setDuration裁良,after延遲會被duration的時間覆蓋,不會產(chǎn)生延遲校套,而且延遲是對play函數(shù)中的動畫來說的价脾,調(diào)用了after(非延遲)之后,after(非延遲)中的動畫是不受after延遲時間影響的笛匙。
animatorSet.play(objectAnimator1).after(objectAnimator2).after(10000);
// animatorSet.setDuration(3000);
animatorSet.start();
4 AnimatorSet Builder的操作方式侨把,每次play,或者鏈?zhǔn)讲僮?/h1>
AnimatorSet 的play函數(shù)和Builder的with妹孙,before秋柄,after函數(shù)都返回Builder 對象實(shí)例,所以我們可每次通過play獲取Builder進(jìn)行動畫操作蠢正,也可以利用Builder函數(shù)返回Builder對象的特性進(jìn)行鏈?zhǔn)讲僮骱П剩瑑煞N操作方式有什么不同呢?
AnimatorSet 的play函數(shù)和Builder的with妹孙,before秋柄,after函數(shù)都返回Builder 對象實(shí)例,所以我們可每次通過play獲取Builder進(jìn)行動畫操作蠢正,也可以利用Builder函數(shù)返回Builder對象的特性進(jìn)行鏈?zhǔn)讲僮骱П剩瑑煞N操作方式有什么不同呢?
重要:
先看AnimatorSet的play方法產(chǎn)生的Builder對象:
* <p>Note that <code>play()</code> is the only way to tell the
* <code>Builder</code> the animation upon which the dependency is created,
* so successive calls to the various functions in <code>Builder</code>
* will all refer to the initial parameter supplied in <code>play()</code>
* as the dependency of the other animations. For example, calling
* <code>play(a1).before(a2).before(a3)</code> will play both <code>a2</code>
* and <code>a3</code> when a1 ends; it does not set up a dependency between
* <code>a2</code> and <code>a3</code>.</p>
public Builder play(Animator anim) {
if (anim != null) {
return new Builder(anim);
}
return null;
}
每次調(diào)用play方法都會產(chǎn)生一個新的Builder對象嚣崭,這個對象約束內(nèi)部動畫的執(zhí)行順序笨触。而且重要的一點(diǎn)是play()方法是唯一一種獲取Builder對象的方式,后續(xù)所有的動畫執(zhí)行都以play方法中傳入的動畫為基準(zhǔn)雹舀,例如:
AnimatorSet s = new AnimatorSet();
s.play(anim1).with(anim2);
s.play(anim2).before(anim3);
s.play(anim4).after(anim3);
動畫anim1 和anim2有關(guān)系芦劣,anim2和anim3有關(guān)系,anim3和anim4有關(guān)系说榆。anim1和anim2將同時執(zhí)行虚吟,anim2先于anim3執(zhí)行,anim3 先于anim4執(zhí)行签财,所以最終的執(zhí)行順序?yàn)閍nim1和anim2同時開始執(zhí)行串慰,anim2執(zhí)行完后開始執(zhí)行anim3,anim3執(zhí)行完后開始執(zhí)行anim4.
代碼示例:
animatorSet.play(objectAnimator1).before(objectAnimator2);
animatorSet.play(objectAnimator1).with(objectAnimator7);
animatorSet.play(objectAnimator7).after(objectAnimator11);
animatorSet.setDuration(3000);
animatorSet.start();
結(jié)果:動畫會先執(zhí)行objectAnimator11(gif中的第三個view變背景),然后objectAnimator1(gif中的第一個view變背景)和objectAnimator7(第二個view變背景)同時執(zhí)行唱蒸,最后執(zhí)行objectAnimator2(縮放)邦鲫;
Builder with before after 函數(shù)返回Builder對象,鏈?zhǔn)讲僮鲃赢?/h2>
/**
* Sets up the given animation to play at the same time as the animation supplied in the
* {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object.
*
* @param anim The animation that will play when the animation supplied to the
* {@link AnimatorSet#play(Animator)} method starts.
*/
public Builder with(Animator anim) {
Node node = getNodeForAnimation(anim);
mCurrentNode.addSibling(node);
return this;
}
/**
* Sets up the given animation to play when the animation supplied in the
* {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
* ends.
*
* @param anim The animation that will play when the animation supplied to the
* {@link AnimatorSet#play(Animator)} method ends.
*/
public Builder before(Animator anim) {
Node node = getNodeForAnimation(anim);
mCurrentNode.addChild(node);
return this;
}
/**
* Sets up the given animation to play when the animation supplied in the
* {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
* to start when the animation supplied in this method call ends.
*
* @param anim The animation whose end will cause the animation supplied to the
* {@link AnimatorSet#play(Animator)} method to play.
*/
public Builder after(Animator anim) {
Node node = getNodeForAnimation(anim);
mCurrentNode.addParent(node);
return this;
}
play之后的鏈?zhǔn)秸{(diào)用不會產(chǎn)生新的Builder對象油宜,會把傳入的動畫添加到play函數(shù)產(chǎn)生的Builder對象的node列表中等待執(zhí)行掂碱。
所以with before after 函數(shù)被調(diào)用之后鏈?zhǔn)讲僮鲃赢嬍遣僮魍粋€Builder對象內(nèi)部的Node鏈,所以都是以play函數(shù)傳入的動畫為基準(zhǔn)慎冤。
play(a1).before(a2).before(a3) a2,a3動畫都是以a1動畫為基準(zhǔn)疼燥,a1動畫執(zhí)行結(jié)束之后a2,a3將同時執(zhí)行,a1動畫和a2,a3有關(guān)系蚁堤,但是a2,a3之間是沒有關(guān)系的醉者。
/**
* Sets up the given animation to play at the same time as the animation supplied in the
* {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object.
*
* @param anim The animation that will play when the animation supplied to the
* {@link AnimatorSet#play(Animator)} method starts.
*/
public Builder with(Animator anim) {
Node node = getNodeForAnimation(anim);
mCurrentNode.addSibling(node);
return this;
}
/**
* Sets up the given animation to play when the animation supplied in the
* {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
* ends.
*
* @param anim The animation that will play when the animation supplied to the
* {@link AnimatorSet#play(Animator)} method ends.
*/
public Builder before(Animator anim) {
Node node = getNodeForAnimation(anim);
mCurrentNode.addChild(node);
return this;
}
/**
* Sets up the given animation to play when the animation supplied in the
* {@link AnimatorSet#play(Animator)} call that created this <code>Builder</code> object
* to start when the animation supplied in this method call ends.
*
* @param anim The animation whose end will cause the animation supplied to the
* {@link AnimatorSet#play(Animator)} method to play.
*/
public Builder after(Animator anim) {
Node node = getNodeForAnimation(anim);
mCurrentNode.addParent(node);
return this;
}
play之后的鏈?zhǔn)秸{(diào)用不會產(chǎn)生新的Builder對象油宜,會把傳入的動畫添加到play函數(shù)產(chǎn)生的Builder對象的node列表中等待執(zhí)行掂碱。
所以with before after 函數(shù)被調(diào)用之后鏈?zhǔn)讲僮鲃赢嬍遣僮魍粋€Builder對象內(nèi)部的Node鏈,所以都是以play函數(shù)傳入的動畫為基準(zhǔn)慎冤。
play(a1).before(a2).before(a3) a2,a3動畫都是以a1動畫為基準(zhǔn)疼燥,a1動畫執(zhí)行結(jié)束之后a2,a3將同時執(zhí)行,a1動畫和a2,a3有關(guān)系蚁堤,但是a2,a3之間是沒有關(guān)系的醉者。
代碼示例看下面:
5 Builder鏈?zhǔn)秸{(diào)用的一些舉例
上面提到調(diào)用play函數(shù)后后續(xù)的所有動畫都是以play傳入的動畫為標(biāo)準(zhǔn)進(jìn)行相應(yīng)操作
下面我們通過幾個列子來說明:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).before(objectAnimator2).before(objectAnimator3).before(objectAnimator4).with(objectAnimator7).with(objectAnimator11);
animatorSet.setDuration(3000);
animatorSet.start();
其中objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4 操作左邊第一個view但狭。
objectAnimator7操作第二個view,
objectAnimator11操作第三個view撬即。
最終的結(jié)果為三個view同時開始動畫也就是objectAnimator1,objectAnimator7,objectAnimator11同時開始執(zhí)行立磁,objectAnimator1執(zhí)行完之后objectAnimator2,objectAnimator3剥槐,objectAnimator4同時開始執(zhí)行唱歧。:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).before(objectAnimator2).before(objectAnimator3).before(objectAnimator4).with(objectAnimator7).with(objectAnimator11).after(objectAnimator12);
animatorSet.setDuration(3000);
animatorSet.start();
其中objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4 操作左邊第一個view。
objectAnimator7操作第二個view粒竖,
objectAnimator11颅崩,objectAnimator12操作第三個view。
結(jié)果:objectAnimator12先執(zhí)行動畫蕊苗,接著objectAnimator1沿后,objectAnimator7,objectAnimator11動畫會同時執(zhí)行朽砰,objectAnimator1執(zhí)行完之后objectAnimator2,objectAnimator3,objectAnimator4動畫會同時執(zhí)行尖滚。
animatorSet.play(objectAnimator1).before(objectAnimator2).after(objectAnimator4).before(objectAnimator3).after(objectAnimator14).after(objectAnimator11);
animatorSet.setDuration(3000);
animatorSet.start();
其中objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4 操作左邊第一個view。
objectAnimator7操作第二個view瞧柔,
objectAnimator11漆弄,objectAnimator12操作第三個view。
上面的代碼會同時執(zhí)行objectAnimator4,objectAnimator11,objectAnimator14,然后執(zhí)行objectAnimator1非剃,再執(zhí)行objectAnimator2 和objectAnimator3
鏈?zhǔn)秸{(diào)用動畫執(zhí)行順序總結(jié)如下:
Builder鏈?zhǔn)秸{(diào)用中會先執(zhí)行after函數(shù)中的動畫(有多個同時執(zhí)行)置逻,然后執(zhí)行play和with函數(shù)(有多個同時執(zhí)行)中的動畫推沸,最后執(zhí)行before函數(shù)中的動畫(有多個同時執(zhí)行)
6 AnimatorSet動畫監(jiān)聽
//如果不想實(shí)現(xiàn)那么多方法备绽,可以利用AnimatorListenerAdapter代替AnimatorListener
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
//需要api19
animatorSet.addPauseListener(new Animator.AnimatorPauseListener() {
@Override
public void onAnimationPause(Animator animation) {
}
@Override
public void onAnimationResume(Animator animation) {
}
});
由于內(nèi)部主要使用ObjectAnimator所以動畫監(jiān)聽顯得不那么重要。
7 AnimationSet xml 文件實(shí)現(xiàn)
用法不多鬓催,只舉個例子(來自developer):
<set android:ordering="sequentially">
<set>
<objectAnimator
android:propertyName="x"
android:duration="500"
android:valueTo="400"
android:valueType="intType"/>
<objectAnimator
android:propertyName="y"
android:duration="500"
android:valueTo="300"
android:valueType="intType"/>
</set>
<objectAnimator
android:propertyName="alpha"
android:duration="500"
android:valueTo="1f"/>
</set>
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.animator.property_animator);
set.setTarget(myObject);
set.start();
Animation動畫概述和執(zhí)行原理
Android動畫之補(bǔ)間動畫TweenAnimation
Android動畫之逐幀動畫FrameAnimation
Android動畫之插值器簡介和系統(tǒng)默認(rèn)插值器
Android動畫之插值器Interpolator自定義
Android動畫之視圖動畫的缺點(diǎn)和屬性動畫的引入
Android動畫之ValueAnimator用法和自定義估值器
Android動畫之ObjectAnimator實(shí)現(xiàn)補(bǔ)間動畫和ObjectAnimator自定義屬性
Android動畫之ObjectAnimator中ofXX函數(shù)全解析-自定義Property肺素,TypeConverter,TypeEvaluator
Android動畫之AnimatorSet聯(lián)合動畫用法
Android動畫之LayoutTransition布局動畫
Android動畫之共享元素動畫
Android動畫之ViewPropertyAnimator(專用于view的屬性動畫)
Android動畫之Activity切換動畫overridePendingTransition實(shí)現(xiàn)和Theme Xml方式實(shí)現(xiàn)
Android動畫之ActivityOptionsCompat概述
Android動畫之場景變換Transition動畫的使用
Android動畫之Transition和TransitionManager使用
Android動畫之圓形揭露動畫Circular Reveal
Android 動畫之 LayoutAnimation 動畫
Android動畫之視圖動畫的缺點(diǎn)和屬性動畫的引入