前兩篇,我為大家講述了利用 XML 來定義動畫及插值器,但在代碼中,我們常常是動態(tài)生成動畫的,所以,這篇將為大家講述如何用代碼生成動態(tài)生成動畫及插值器。
先簡單寫出各個標簽對應的類,方便大家理解:
官方 SDK 講解頁面為:《Animation》
第一篇中我們提到過,Animation 類是所有動畫(scale、alpha、translate、rotate)的基類,它所具有的標簽及對應函數(shù)為:
在第一篇《 Animation 動畫詳解(一)——alpha、scale、translate、rotate、set 的 xml 屬性及用法》 我們已經(jīng)講解了每個標簽具體所具有的功能,這里就不再細講,對于使用方法會在下面的各標簽中使用。
這是 scale 標簽對應的類,官方 SDK 頁面為:《ScaleAnimation》
在 Scale 標簽中,我們提到過它的自有屬性有下面幾條,先列一下:
第一個構造函數(shù)是從本地 XML 文件加載動畫,基本用不到的,我們主要看下面三個構造函數(shù)。 在標簽屬性 android:pivotX 中有三種取值,數(shù),百分數(shù),百分數(shù) p;體現(xiàn)在構造函數(shù)中,就是最后一個構造函數(shù)的 pivotXType,它的取值有三個,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF 和 Animation.RELATIVE_TO_PARENT;
這三個構造函數(shù)難度不大,就不再細講,舉個例子說明:
在第一篇中 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" />
對應的代碼構造代碼為:
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);
在控件使用的時候,同樣是使用:
tv.startAnimation(scaleAnim);
這是 alpha 標簽對就的類,官方 SDK 文檔地址是:《AlphaAnimation》 同樣 alpha 標簽自有的屬性有:
<?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>
如果用代碼構造同樣的效果,它所對應的代碼為:
alphaAnim = new AlphaAnimation(1.0f,0.1f);
alphaAnim.setDuration(3000);
alphaAnim.setFillBefore(true);
RotateAnimation 類對應 Rotate 標簽,SDK 文檔地址:《RotateAnimation》
Rotate 標簽所具有的 XML 屬性有:
根據(jù)每一篇中的 XML 寫出對應的 JAVA 構造代碼:
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>
對應 JAVA 構造代碼為:
rotateAnim = new RotateAnimation(0, -650, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(3000);
rotateAnim.setFillAfter(true);
很顯示 TranslateAnimation 類對應 translate 標簽,它的 SDK 官方文檔地址為:《TranslateAnimation》 translate 標簽所具有的屬性為:
下面以第一篇中的 XML 代碼為例,用 JAVA 代碼構造同樣的效果:
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>
對應的 JAVA 代碼為:
translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80,
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -80);
translateAnim.setDuration(2000);
translateAnim.setFillBefore(true);
AnimationSet 類對應 set 標簽,定義動作類的集合,對應的 SDK 文檔地址為:《AnimationSet》 它自己是沒有 XML 屬性的,所以我們直接說它的構造函數(shù):
AnimationSet(boolean shareInterpolator) shareInterpolator 取值 true 或 false,取 true 時,指在 AnimationSet 中定義一個插值器(interpolater),它下面的所有動畫共同。如果設為 false,則表示它下面的動畫自己定義各自的插值器。 增加動畫的函數(shù)為:(更多函數(shù),請參看 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>
對應的 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);
關于插值器的效果及應用,我們專門開了一篇來講,看這里:《Animation 動畫詳解(二)——Interpolator 插值器》 關于插值器的 SDK 講解見《Animation Resources》中的 Interpolators 部分;
插值器 XML 屬性及對應的類如下表所示:
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);
下面我把上面所有的代碼集合到一個例子中,供大家下載; 效果圖如下:
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
請大家尊重原創(chuàng)者版權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/40117115 謝謝!