關於動畫的實現,Android提供了Animation,在Android SDK介紹了2種Animation模式: 1. Tween Animation:通過對場景裏的對象不斷做圖像變換(平移、縮放、旋轉)產生動畫效果,即是一種漸變動畫; 2. Frame Animation:順序播放事先做好的圖像,是一種畫面轉換動畫。 動畫類型下面先來看看Android提供的動畫類型。Android的animation由四種類型組成 在XML文件中:
在Java 源碼中定義了相應的類,可以使用這些類的方法來獲取和操作相應的屬性:
Tween Animation一個tween動畫將對視圖對象中的內容進行一系列簡單的轉換(位置,大小,旋轉,透明性)。如果你有一個文本視圖對象,你可以移動它,旋轉它,讓它變大或讓它變小,如果文字下面還有背景圖像,背景圖像也會隨着文件進行轉換。 使用XML來定義Tween Animation 動畫的XML文件在工程中res/anim目錄,這個文件必須包含一個根元素,可以使<alpha><scale> <translate> <rotate>插值元素或者是把上面的元素都放入<set>元素組中,默認情況下,所以的動畫指令都是同時發生的,爲了讓他們按序列發生,需要設置一個特殊的屬性startOffset。動畫的指令定義了你想要發生什麼樣的轉換,當他們發生了,應該執行多長時間,轉換可以是連續的也可以使同時的。例如,你讓文本內容從左邊移動到右邊,然後旋轉180度,或者在移動的過程中同時旋轉,沒個轉換需要設置一些特殊的參數(開始和結束的大小尺寸的大小變化,開始和結束的旋轉角度等等,也可以設置些基本的參數(例如,開始時間與週期),如果讓幾個轉換同時發生,可以給它們設置相同的開始時間,如果按序列的話,計算開始時間加上其週期。 Tween Animation共同的節點屬性
下面給出一個完整的XML定義(SDK提供) <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="700" /> <set android:interpolator="@android:anim/decelerate_interpolator"> <scale android:fromXScale="1.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" android:fillBefore="false" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:duration="400" /> </set> </set> Tween Animation如何使用 使用AnimationUtils類的靜態方法loadAnimation()來加載XML中的動畫XML文件 //main.xml中的ImageView ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); //加載動畫 Animation hyperspaceJumpAnimation =AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); //使用ImageView顯示動畫 spaceshipImage.startAnimation(hyperspaceJumpAnimation); 如何在Java代碼中定義動畫 //在代碼中定義 動畫實例對象 private Animation myAnimation_Alpha; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation myAnimation_Rotate; //根據各自的構造方法來初始化一個實例對象 myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f); myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f); myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f); interpolator的解釋 interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重複等。
Interpolator 定義了動畫的變化速度,可以實現勻速、正加速、負加速、無規則變加速等。Interpolator 是基類,封裝了所有 Interpolator 的共同方法,它只有一個方法,即 getInterpolation (float input),該方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了幾個 Interpolator 子類,實現了不同的速度曲線,如下:
Frame AnimationFrame Animation是順序播放事先做好的圖像,跟電影類似。不同於animation package, Android SDK提供了另外一個類AnimationDrawable來定義、使用Frame Animation。 Frame Animation可以在XML Resource定義(還是存放到res\anim文件夾下),也可以使用AnimationDrawable中的API定義。由於Tween Animation與Frame Animation有着很大的不同,因此XML定義的格式也完全不一樣,其格式是:首先是animation-list根節點,animation-list根節點中包含多個item子節點,每個item節點定義一幀動畫,當前幀的drawable資源和當前幀持續的時間。下面對節點的元素加以說明:
下面就給個具體的XML例子,來定義一幀一幀的動畫: <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 上面的XML就定義了一個Frame Animation,其包含3幀動畫,3幀動畫中分別應用了drawable中的3張圖片:rocket_thrust1,rocket_thrust2,rocket_thrust3,每幀動畫持續200毫秒。 然後我們將以上XML保存在res/anim/文件夾下,命名爲rocket_thrust.xml,顯示動畫的代碼: AnimationDrawable rocketAnimation; 代碼運行的結果:3張圖片按照順序的播放一次. 有一點需要強調的是:啓動Frame Animation動畫的代碼rocketAnimation.start();不能在OnCreate()中,因爲在OnCreate()中AnimationDrawable還沒有完全的與ImageView綁定,在OnCreate()中啓動動畫,就只能看到第一張圖片。這裏實在拖曳事件中實現的。 下面,閱讀Android SDK中對AnimationDrawable的介紹,有個簡單的瞭解:
|
Android anim動畫切換效果
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.