Android 動畫AnimationDrawable資源 的使用方法


【AnimationDrawable資源基礎知識】


 AnimationDrawable代表一個動畫。

       下面以補間動畫為例來介紹如何定義AnimationDrawable資源,定義補間動畫的XML資源文件已<set.../>元素作為根元素,該元素可以指定如下4個元素:

  • alpha:設置透明度的改變。
  • scale:設置圖片進行縮放改變。
  • translate:設置圖片進行位移變換。
  • roate:設置圖片進行旋轉。    

        定義動畫的XML資源應該放在/res/anmi/路徑下,當使用ADT創建一個Android應用時默認不會包含該路徑,開發者需要自行創建該路徑。

        定義補間動畫的思路很簡單:設置一張圖片的開始狀態(包括透明度、位置、縮放比、旋轉度)、並設置該圖片的結束狀態(包括透明度、位置、縮放比、旋轉度),再設置動畫的持續時間,Android系統會使用動畫效果把這張圖片從開始狀態變換到結束狀態。

        設置補間動畫的語法格式如下:

 

 
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true"|"false"] android:duration="持續時間"
android:fillAfter=["true"|"false"]> //設置動畫結束后,保留結束狀態
<alpha android:fromAlpha="float"
android:toAlpha="float"/> <!-- 定義縮放變換 --> <scale android:fromXScale="flaot" android:toXScale="flaot" android:fromYScale="flaot" android:toYScale="flaot" android:pivotX="flaot" android:pivotY="flaot" /> <!-- 定義為移變換 --> <translate android:fromXDelta="flaot" android:toXDelta="flaot" android:fromYDelta="flaot" android:toYDelta="flaot" /> <rotate android:fromDegrees="float" android:toDegrees="float" android:pivotX="float" android:pivotY="float"/> </set>
 

 

【解釋】

1、上面的語法格式中包含了大量的fromXxx、toXxx屬性,這些屬性就用於定義圖片的開始狀態、結束狀態。除此之外,當進行縮放變換(scale)、旋轉(roate)變換時,還需要指定如下pivotX、pivotY兩個屬性,這兩個屬性用於指定變換的“中心點”——比如進行旋轉變換時,需要指定“旋軸點”;進行縮放變換時需要指定“中心點”。

2、除此之外,上面<set.../>、<alpha.../>、<scale.../>、<translate.../>、<rotate.../>都可指定一個android:interpolator屬性,該屬性指定動畫的變化速度,可實現勻速、正加速、負加速、無規則變加速等,Android系統的R.anim類中包含了許多常量,它們定義了不同的動畫速度,例如:

  • linear_interpolator:勻速變換。
  • accelerate_interpolator:加速變換。
  • decelerate_interpolator:減速變換。  

3、如果程序想讓<set.../>元素下所有的變換效果使用相同的動畫加速,則可指定android:shareInterpolator="true"

4、通過上面的介紹,我們就能夠創建一個動畫xml文件,之后我們就能夠將這個動畫文件應用在指定的View上,在可以使用動畫的組件View的方法中會有專門用於加載動畫xml文件的方法,只要將R.anim.filename 作為參數賦給它即可完成,這一點我們不用擔心。除此之外,有些能夠加載動畫的View組件的方法中的接收參數不是 xml 資源文件,而是一個Animation類型的參數,那么我們就需要將我們創建好的動畫xml文件轉化成Animation對象,這也非常的簡單:

為了在Java代碼中獲取實際的Animation對象,則可調用AnimationUtils類的如下靜態方法:

AnimationUtils.loadAnimation(Context ctx,int resId)   

 5、給屬性的單位的設定:

 ①

<alpha android:fromAlpha="float" 
           android:toAlpha="float"/>
范圍從 0.0 —— 1.0

 ②

<scale android:fromXScale="flaot"  
         android:toXScale="flaot"

         android:fromYScale="flaot"
         android:toYScale="flaot"

范圍從 0.0 —— 1.0


         android:pivotX="flaot"
         android:pivotY="flaot"

 

android:pivotX的值共有三種設置方法:

1.android:pivot="50"這種方法使用絕對位置定位,屏幕右下角的座標是X:320,Y:480
2.android:pivot="50%"這種方法相對於控件本身定位 , 范圍從 -100% 到 +100%
3.android:pivot="50%p"這種方法相對於控件的父控件定位 , 范圍從 -100%p 到 +100%p

/>

③位移

<translate android:fromXDelta="flaot"
         android:toXDelta="flaot"
         android:fromYDelta="flaot"
         android:toYDelta="flaot"/>

四個值和上面的三種設置方式的值一樣

 

④旋轉

<rotate android:fromDegrees="float"
         android:toDegrees="float"

數值范圍從 -360  到 +360


         android:pivotX="float"
         android:pivotY="float"

數值的設定方式和上面的三種方式一樣

/>

 

【實例】

 程序清單文件:\res\anim\my_anim.xml文件

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:shareInterpolator="true" android:duration="5000"> <!-- 定義縮放變換 --> <scale android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:duration="2000"/> <!-- 定義為移變換 --> <translate android:fromXDelta="10" android:toXDelta="130" android:fromYDelta="30" android:toYDelta="-80" android:duration="2000"/> </set>

界面布局文件如下:

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/java" /> <Button android:id="@+id/bn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始動畫"/> </LinearLayout>

 

后台代碼文件如下:

 
package com.example.studyresources; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class AnimationDrawableTest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animation_drawable_test); final ImageView image=(ImageView)findViewById(R.id.image); //加載動畫資源 final Animation anim=AnimationUtils.loadAnimation(this,R.anim.my_anim); //設置動畫結束后,保留結束狀態 anim.setFillAfter(true);//① Button bn=(Button)findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub //開始動畫  image.startAnimation(anim); }}); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.animation_drawable_test, menu); return true; } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM