鍍金池/ 教程/ Android/ 代碼生成 alpha、scale、translate、rotate、set 及插值器動畫
聯(lián)合動畫的 XML 實現(xiàn)與使用示例
Interpolator 插值器
高級進階(二)
ObjectAnimator 基本使用
ValueAnimator 基本使用
alpha、scale、translate、rotate、set 的 xml 屬性及用法
PropertyValuesHolder 與 Keyframe
layoutAnimation 與 gridLayoutAnimation
自定義控件三部曲之動畫篇(十三)——實現(xiàn)ListView Item進入動畫
自定義控件三部曲之動畫篇(十二)——animateLayoutChanges與LayoutTransition
高級進階(一)
代碼生成 alpha、scale、translate、rotate、set 及插值器動畫
聯(lián)合動畫的代碼實現(xiàn)

代碼生成 alpha、scale、translate、rotate、set 及插值器動畫

一、概述

前兩篇,我為大家講述了利用 XML 來定義動畫及插值器,但在代碼中,我們常常是動態(tài)生成動畫的,所以,這篇將為大家講述如何用代碼生成動態(tài)生成動畫及插值器。

先簡單寫出各個標簽對應(yīng)的類,方便大家理解:

  • scale —— ScaleAnimation
  • alpha —— AlphaAnimation
  • rotate —— RotateAnimation
  • translate —— TranslateAnimation
  • set —— AnimationSet

二、Animation 公共類

官方 SDK 講解頁面為:《Animation》

第一篇中我們提到過,Animation 類是所有動畫(scale、alpha、translate、rotate)的基類,它所具有的標簽及對應(yīng)函數(shù)為:

  • android:duration setDuration(long) 動畫持續(xù)時間,以毫秒為單位
  • android:fillAfter setFillAfter(boolean) 如果設(shè)置為 true,控件動畫結(jié)束時,將保持動畫最后時的狀態(tài)
  • android:fillBefore setFillBefore(boolean) 如果設(shè)置為 true,控件動畫結(jié)束時,還原到開始動畫前的狀態(tài)
  • android:fillEnabled setFillEnabled(boolean) 與 android:fillBefore 效果相同,都是在動畫結(jié)束時,將控件還原到初始化狀態(tài)
  • android:repeatCount setRepeatCount(int) 重復次數(shù)
  • android:repeatMode setRepeatMode(int) 重復類型,有 reverse 和 restart 兩個值,取值為 RESTART 或 REVERSE,必須與 repeatCount 一起使用才能看到效果。因為這里的意義是重復的類型,即回放時的動作。
  • android:interpolator setInterpolator(Interpolator) 設(shè)定插值器,其實就是指定的動作效果,比如彈跳效果等

在第一篇《 Animation 動畫詳解(一)——alpha、scale、translate、rotate、set 的 xml 屬性及用法》 我們已經(jīng)講解了每個標簽具體所具有的功能,這里就不再細講,對于使用方法會在下面的各標簽中使用。

三、ScaleAnimation

這是 scale 標簽對應(yīng)的類,官方 SDK 頁面為:《ScaleAnimation》

在 Scale 標簽中,我們提到過它的自有屬性有下面幾條,先列一下:

  • android:fromXScale 起始的 X 方向上相對自身的縮放比例,浮點值,比如 1.0 代表自身無變化,0.5 代表起始時縮小一倍,2.0 代表放大一倍;
  • android:toXScale 結(jié)尾的 X 方向上相對自身的縮放比例,浮點值;
  • android:fromYScale 起始的 Y 方向上相對自身的縮放比例,浮點值,
  • android:toYScale 結(jié)尾的 Y 方向上相對自身的縮放比例,浮點值;
  • android:pivotX 縮放起點 X 軸坐標,可以是數(shù)值、百分數(shù)、百分數(shù) p 三種樣式,比如 50、50%、50%p,當為數(shù)值時,表示在當前 View 的左上角,即原點處加上 50px,做為起始縮放點;如果是 50%,表示在當前控件的左上角加上自己寬度的 50%做為起始點;如果是 50%p,那么就是表示在當前的左上角加上父控件寬度的 50%做為起始點 x 軸坐標。(具體意義,后面會舉例演示)
  • android:pivotY 縮放起點 Y 軸坐標,取值及意義跟 android:pivotX 一樣。 放到代碼中,ScaleAnimation 有下面幾個構(gòu)造函數(shù):
  • ScaleAnimation(Context context, AttributeSet attrs) 從 XML 文件加載動畫,基本用不到
  • ScaleAnimation(float fromX, float toX, float fromY, float toY)
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

第一個構(gòu)造函數(shù)是從本地 XML 文件加載動畫,基本用不到的,我們主要看下面三個構(gòu)造函數(shù)。 在標簽屬性 android:pivotX 中有三種取值,數(shù),百分數(shù),百分數(shù) p;體現(xiàn)在構(gòu)造函數(shù)中,就是最后一個構(gòu)造函數(shù)的 pivotXType,它的取值有三個,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF 和 Animation.RELATIVE_TO_PARENT;

這三個構(gòu)造函數(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" />  

對應(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);  

在控件使用的時候,同樣是使用:

tv.startAnimation(scaleAnim);  

四、AlphaAnimation

這是 alpha 標簽對就的類,官方 SDK 文檔地址是:《AlphaAnimation》 同樣 alpha 標簽自有的屬性有:

  • android:fromAlpha 動畫開始的透明度,從 0.0 --1.0 ,0.0 表示全透明,1.0 表示完全不透明
  • android:toAlpha 動畫結(jié)束時的透明度,也是從 0.0 --1.0 ,0.0 表示全透明,1.0 表示完全不透明 所對應(yīng)的構(gòu)造函數(shù)為:
  • AlphaAnimation(Context context, AttributeSet attrs) 同樣,從本地 XML 加載動畫,基本不用
  • AlphaAnimation(float fromAlpha, float toAlpha) 這里只剩最后一個構(gòu)造函數(shù),難度不大,下面舉個例子說明下用法。 在第一篇文章中,我們構(gòu)造的 XML 代碼為:
<?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)造同樣的效果,它所對應(yīng)的代碼為:

alphaAnim = new AlphaAnimation(1.0f,0.1f);  
alphaAnim.setDuration(3000);  
alphaAnim.setFillBefore(true);  

五、RotateAnimation

RotateAnimation 類對應(yīng) Rotate 標簽,SDK 文檔地址:《RotateAnimation》

Rotate 標簽所具有的 XML 屬性有:

  • android:fromDegrees 開始旋轉(zhuǎn)的角度位置,正值代表順時針方向度數(shù),負值代碼逆時針方向度數(shù)
  • android:toDegrees 結(jié)束時旋轉(zhuǎn)到的角度位置,正值代表順時針方向度數(shù),負值代碼逆時針方向度數(shù)
  • android:pivotX 縮放起點 X 軸坐標,可以是數(shù)值、百分數(shù)、百分數(shù) p 三種樣式,比如 50、50%、50%p,具體意義已在 scale 標簽中講述,這里就不再重講
  • android:pivotY 縮放起點 Y 軸坐標,可以是數(shù)值、百分數(shù)、百分數(shù) p 三種樣式,比如 50、50%、50%p 對應(yīng)的構(gòu)造函數(shù)有:
  • RotateAnimation(Context context, AttributeSet attrs)  從本地 XML 文檔加載動畫,同樣,基本不用
  • RotateAnimation(float fromDegrees, float toDegrees)
  • RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
  • RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) RotateAnimation 跟 ScaleAnimation 差不多,關(guān)鍵問題同樣是 pivotXType 和 pivotYType 的選擇,同樣有三個取值:Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF 和 Animation.RELATIVE_TO_PARENT;

根據(jù)每一篇中的 XML 寫出對應(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>  

對應(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

很顯示 TranslateAnimation 類對應(yīng) translate 標簽,它的 SDK 官方文檔地址為:《TranslateAnimation》 translate 標簽所具有的屬性為:

  • android:fromXDelta 起始點 X 軸坐標,可以是數(shù)值、百分數(shù)、百分數(shù) p 三種樣式,比如 50、50%、50%p,具體意義已在 scale 標簽中講述,這里就不再重講
  • android:fromYDelta 起始點 Y 軸從標,可以是數(shù)值、百分數(shù)、百分數(shù) p 三種樣式;
  • android:toXDelta 結(jié)束點 X 軸坐標
  • android:toYDelta 結(jié)束點 Y 軸坐標 這些屬性所對應(yīng)的構(gòu)造函數(shù)為:
  • TranslateAnimation(Context context, AttributeSet attrs) 同樣,基本不用
  • TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
  • TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 由于 fromXDelta、fromYDelta、toXDelta、toYDelta 這三個屬性都具有三種狀態(tài),所以在構(gòu)造函數(shù)中,最理想的狀態(tài)就是第三個構(gòu)造函數(shù),能夠指定每個值的類型,第二個構(gòu)造函數(shù):TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)使用是絕對數(shù)值。只有最后一個構(gòu)造函數(shù)可以指定百分數(shù)和相對父控件的百分數(shù)。

下面以第一篇中的 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>  

對應(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

AnimationSet 類對應(yīng) set 標簽,定義動作類的集合,對應(yīng)的 SDK 文檔地址為:《AnimationSet》 它自己是沒有 XML 屬性的,所以我們直接說它的構(gòu)造函數(shù):

  • AnimationSet(Context context, AttributeSet attrs) 同樣,基本不用
  • AnimationSet(boolean shareInterpolator) shareInterpolator 取值 true 或 false,取 true 時,指在 AnimationSet 中定義一個插值器(interpolater),它下面的所有動畫共同。如果設(shè)為 false,則表示它下面的動畫自己定義各自的插值器。 增加動畫的函數(shù)為:(更多函數(shù),請參看 SDK 文檔)

  • public void addAnimation (Animation a) 下面在第一篇中的 XML 代碼為例寫出能構(gòu)造同樣效果的 JAVA 代碼:

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> 

對應(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); 

八、Interpolater 插值器

關(guān)于插值器的效果及應(yīng)用,我們專門開了一篇來講,看這里:《Animation 動畫詳解(二)——Interpolator 插值器》 關(guān)于插值器的 SDK 講解見《Animation Resources》中的 Interpolators 部分;

插值器 XML 屬性及對應(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);  

九、示例,源碼

下面我把上面所有的代碼集合到一個例子中,供大家下載; 效果圖如下:

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)者版權(quán),轉(zhuǎn)載請標明出處:http://blog.csdn.net/harvic880925/article/details/40117115 謝謝!