前兩篇,我為大家講述了利用 XML 來(lái)定義動(dòng)畫(huà)及插值器,但在代碼中,我們常常是動(dòng)態(tài)生成動(dòng)畫(huà)的,所以,這篇將為大家講述如何用代碼生成動(dòng)態(tài)生成動(dòng)畫(huà)及插值器。
先簡(jiǎn)單寫(xiě)出各個(gè)標(biāo)簽對(duì)應(yīng)的類,方便大家理解:
官方 SDK 講解頁(yè)面為:《Animation》
第一篇中我們提到過(guò),Animation 類是所有動(dòng)畫(huà)(scale、alpha、translate、rotate)的基類,它所具有的標(biāo)簽及對(duì)應(yīng)函數(shù)為:
在第一篇《 Animation 動(dòng)畫(huà)詳解(一)——alpha、scale、translate、rotate、set 的 xml 屬性及用法》 我們已經(jīng)講解了每個(gè)標(biāo)簽具體所具有的功能,這里就不再細(xì)講,對(duì)于使用方法會(huì)在下面的各標(biāo)簽中使用。
這是 scale 標(biāo)簽對(duì)應(yīng)的類,官方 SDK 頁(yè)面為:《ScaleAnimation》
在 Scale 標(biāo)簽中,我們提到過(guò)它的自有屬性有下面幾條,先列一下:
第一個(gè)構(gòu)造函數(shù)是從本地 XML 文件加載動(dòng)畫(huà),基本用不到的,我們主要看下面三個(gè)構(gòu)造函數(shù)。 在標(biāo)簽屬性 android:pivotX 中有三種取值,數(shù),百分?jǐn)?shù),百分?jǐn)?shù) p;體現(xiàn)在構(gòu)造函數(shù)中,就是最后一個(gè)構(gòu)造函數(shù)的 pivotXType,它的取值有三個(gè),Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF 和 Animation.RELATIVE_TO_PARENT;
這三個(gè)構(gòu)造函數(shù)難度不大,就不再細(xì)講,舉個(gè)例子說(shuō)明:
在第一篇中 Scale 的例子的 XML 代碼為:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50"
android:pivotY="50"
android:duration="700" />
對(duì)應(yīng)的代碼構(gòu)造代碼為:
scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scaleAnim.setDuration(700);
在控件使用的時(shí)候,同樣是使用:
tv.startAnimation(scaleAnim);
這是 alpha 標(biāo)簽對(duì)就的類,官方 SDK 文檔地址是:《AlphaAnimation》 同樣 alpha 標(biāo)簽自有的屬性有:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="3000"
android:fillBefore="true">
</alpha>
如果用代碼構(gòu)造同樣的效果,它所對(duì)應(yīng)的代碼為:
alphaAnim = new AlphaAnimation(1.0f,0.1f);
alphaAnim.setDuration(3000);
alphaAnim.setFillBefore(true);
RotateAnimation 類對(duì)應(yīng) Rotate 標(biāo)簽,SDK 文檔地址:《RotateAnimation》
Rotate 標(biāo)簽所具有的 XML 屬性有:
根據(jù)每一篇中的 XML 寫(xiě)出對(duì)應(yīng)的 JAVA 構(gòu)造代碼:
XML 為:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-650"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
android:fillAfter="true">
</rotate>
對(duì)應(yīng) JAVA 構(gòu)造代碼為:
rotateAnim = new RotateAnimation(0, -650, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(3000);
rotateAnim.setFillAfter(true);
很顯示 TranslateAnimation 類對(duì)應(yīng) translate 標(biāo)簽,它的 SDK 官方文檔地址為:《TranslateAnimation》 translate 標(biāo)簽所具有的屬性為:
下面以第一篇中的 XML 代碼為例,用 JAVA 代碼構(gòu)造同樣的效果:
XML 代碼:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-80"
android:fromYDelta="0"
android:toYDelta="-80"
android:duration="2000"
android:fillBefore="true">
</translate>
對(duì)應(yīng)的 JAVA 代碼為:
translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);
translateAnim.setDuration(2000);
translateAnim.setFillBefore(true);
AnimationSet 類對(duì)應(yīng) set 標(biāo)簽,定義動(dòng)作類的集合,對(duì)應(yīng)的 SDK 文檔地址為:《AnimationSet》 它自己是沒(méi)有 XML 屬性的,所以我們直接說(shuō)它的構(gòu)造函數(shù):
AnimationSet(boolean shareInterpolator) shareInterpolator 取值 true 或 false,取 true 時(shí),指在 AnimationSet 中定義一個(gè)插值器(interpolater),它下面的所有動(dòng)畫(huà)共同。如果設(shè)為 false,則表示它下面的動(dòng)畫(huà)自己定義各自的插值器。 增加動(dòng)畫(huà)的函數(shù)為:(更多函數(shù),請(qǐng)參看 SDK 文檔)
XML 代碼為:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<scale
android:fromXScale="0.0"
android:toXScale="1.4"
android:fromYScale="0.0"
android:toYScale="1.4"
android:pivotX="50%"
android:pivotY="50%"/>
<rotate
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
對(duì)應(yīng)的 JAVA 代碼為:
alphaAnim = new AlphaAnimation(1.0f,0.1f);
scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
setAnim=new AnimationSet(true);
setAnim.addAnimation(alphaAnim);
setAnim.addAnimation(scaleAnim);
setAnim.addAnimation(rotateAnim);
setAnim.setDuration(3000);
setAnim.setFillAfter(true);
關(guān)于插值器的效果及應(yīng)用,我們專門(mén)開(kāi)了一篇來(lái)講,看這里:《Animation 動(dòng)畫(huà)詳解(二)——Interpolator 插值器》 關(guān)于插值器的 SDK 講解見(jiàn)《Animation Resources》中的 Interpolators 部分;
插值器 XML 屬性及對(duì)應(yīng)的類如下表所示:
http://wiki.jikexueyuan.com/project/android-animation/images/23.png" alt="" />
使用方法:(為 sacleAnimation 增加 bounce 插值器)
ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
interpolateScaleAnim.setInterpolator(new BounceInterpolator());
interpolateScaleAnim.setDuration(3000);
下面我把上面所有的代碼集合到一個(gè)例子中,供大家下載; 效果圖如下:
http://wiki.jikexueyuan.com/project/android-animation/images/51.gif" alt="" />
http://wiki.jikexueyuan.com/project/android-animation/images/52.gif" alt="" />
源碼下載地址:http://download.csdn.net/detail/harvic880925/8047669
請(qǐng)大家尊重原創(chuàng)者版權(quán),轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/harvic880925/article/details/40117115 謝謝!