XML DOM 獲取節(jié)點(diǎn)值
870
2025-03-31
本文參考Android屬性動(dòng)畫(huà)完全解析(上),初識(shí)屬性動(dòng)畫(huà)的基本用法
android3.0之前一共有兩種動(dòng)畫(huà),分別是frame動(dòng)畫(huà)和tween動(dòng)畫(huà),關(guān)于這兩種動(dòng)畫(huà)如果不了解可以查看我之前的文章android之frame動(dòng)畫(huà)詳解 和android之tween動(dòng)畫(huà)詳解 ,frame動(dòng)畫(huà)就是逐幀動(dòng)畫(huà),把多張圖片放在一起連續(xù)播放實(shí)現(xiàn)一種類(lèi)似于gif圖片的動(dòng)畫(huà)效果,tween動(dòng)畫(huà)就是補(bǔ)間動(dòng)畫(huà),主要是對(duì)圖像進(jìn)行平移,縮放,旋轉(zhuǎn),改變透明度等。然而tween動(dòng)畫(huà)有很大的局限性,我們看看官方文檔:
tween動(dòng)畫(huà)被稱(chēng)作View Animation,第一句就說(shuō)的很清楚了,你可以使用View Animation System 來(lái)在View上實(shí)現(xiàn)tween動(dòng)畫(huà)。也就是說(shuō)tween動(dòng)畫(huà)只能作用在view上,可是如果我們想變換一個(gè)自定義View的顏色該怎么辦?抱歉,tween無(wú)法做到。
android3.0推出了一種新的動(dòng)畫(huà)實(shí)現(xiàn)方式,那就是屬性動(dòng)畫(huà),屬性動(dòng)畫(huà),顧名思義,就是作用在View的屬性上,我們可以讓View的屬性實(shí)現(xiàn)動(dòng)畫(huà)效果。
說(shuō)到這里我們不得不介紹ObjectAnimator類(lèi),這個(gè)也是在實(shí)際開(kāi)發(fā)中用的最多的,追根溯源,我們發(fā)現(xiàn)ObjectAnimator繼承自ValueAnimator,ValueAnimator可以幫助我們實(shí)現(xiàn)一些簡(jiǎn)單的數(shù)值的變化,不過(guò)到目前為止還沒(méi)用上這個(gè)東東。
下面我們先來(lái)看看怎么使用屬性動(dòng)畫(huà)來(lái)實(shí)現(xiàn)tween動(dòng)畫(huà)效果:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0.5f); animator.setDuration(5000); animator.start();
1
2
3
4
我們通過(guò)ofFloat方法來(lái)獲得一個(gè)ObjectAnimator實(shí)例,先看看該方法的源碼:
public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) { ObjectAnimator anim = new ObjectAnimator(target, propertyName); anim.setFloatValues(values); return anim; }
1
2
3
4
5
從源碼中可以看到該方法接收N個(gè)參數(shù),第一個(gè)是我們要設(shè)置動(dòng)畫(huà)的對(duì)象,第二個(gè)參數(shù)給哪個(gè)屬性設(shè)置動(dòng)畫(huà),后面兩個(gè)參數(shù)表示控件從不透明變?yōu)榘胪该鳎竺娴膮?shù)可以傳N多個(gè),比如
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0.5f,1f,0f);
1
2
表示控件從不透明到半透明再到不透明,最后到全透明這樣一個(gè)狀態(tài)。有了這個(gè)例子之后,我想要實(shí)現(xiàn)其他tween動(dòng)畫(huà)實(shí)現(xiàn)的效果都不是問(wèn)題了。
旋轉(zhuǎn):
ObjectAnimator animator2 = ObjectAnimator.ofFloat(tv2, "rotation", 0f, 360f); animator2.setDuration(5000); animator2.start();
1
2
3
4
平移:
float curTranslationX = tv.getTranslationX(); ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", curTranslationX, -1000f, curTranslationX); animator.setDuration(3000); animator.start();
1
2
3
4
5
如果要實(shí)現(xiàn)組合動(dòng)畫(huà)效果呢?
ObjectAnimator moveIn = ObjectAnimator.ofFloat(tv, "translationX", -500f, 0f); ObjectAnimator rotate = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f); ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0f, 1f); AnimatorSet animSet = new AnimatorSet(); animSet.play(rotate).with(fadeInOut).after(moveIn); animSet.setDuration(5000); animSet.start();
1
2
3
4
5
6
7
8
9
10
哈哈,還是很簡(jiǎn)單吧。
這里有一個(gè)需要注意的地方就是:
// after(Animator anim) 現(xiàn)有動(dòng)畫(huà)在傳入的動(dòng)畫(huà)之后執(zhí)行 // after(long delay) 現(xiàn)有動(dòng)畫(huà)延遲指定毫秒后執(zhí)行 // before(Animator anim) 現(xiàn)有動(dòng)畫(huà)在傳入的動(dòng)畫(huà)之前執(zhí)行 // with(Animator anim) 現(xiàn)有動(dòng)畫(huà)和傳入的動(dòng)畫(huà)同時(shí)執(zhí)行
1
2
3
4
當(dāng)然,屬性動(dòng)畫(huà)和tween動(dòng)畫(huà)一樣,即可以使用代碼實(shí)現(xiàn)也可以使用xml來(lái)實(shí)現(xiàn),那么我們看看怎么通過(guò)xml文件來(lái)實(shí)現(xiàn)屬性動(dòng)畫(huà):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
android:ordering的值為together表示各個(gè)維度的變化同時(shí)發(fā)生,缺省值也是together,sequentially表示動(dòng)畫(huà)依次發(fā)生。
基本上就是這樣。
Android
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。