Android開發之動畫實現
/*
* Android開發之動畫實現
* 北京Android俱樂部群:167839253
* Created on: 2011-12-09
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
在《Android開發之PopupWindow》這篇文章中,已經初步涉及到了動畫的相關內容。對於一個彈出對話方塊。其動畫效果可以利用xml檔案進行設定,複習一下,就是對於對話方塊的飛進飛出,定義兩個XML檔案。例如:
飛入動畫xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromYDelta="-100" android:toYDelta="0" android:duration="1000" android:fillEnabled="true" android:fillAfter="true" /> <scale android:fromXScale="0.6" android:toXScale="1.0" android:fromYScale="0.6" android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:fillAfter="false"/> <alpha android:interpolator="@android:anim/decelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" /> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="+359" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> </rotate></set>
anin.xml
飛出動畫:
anout.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromYDelta="0" android:toYDelta="-100" android:duration="1000" android:fillEnabled="true" android:fillAfter="true" /> <scale android:fromXScale="1.0" android:toXScale="0.4" android:fromYScale="1.0" android:toYScale="0.4" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> <alpha android:interpolator="@android:anim/decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" /></set>
總結一下上面動畫的特點:
1. 透明度漸層
2. 尺寸伸縮
3. 畫面旋轉
4. 位置移動
那麼這類動畫,可以在xml中體現,當然也可以在代碼中的體現。在代碼中對於上述四種動畫形式可以總結成:
1. 透明度漸層
AlphaAnimation(float fromAlpha,float toAlpha);
這裡的參數跟xml檔案中的參數為對應的,(後面的也均為對應)功能跟xml檔案中的保持一致。
fromAlpha:表示動畫起始時的透明度;
toAlpha:表示動畫結束時的透明度;
0.0表示完全透明,1.0表示完全不透明。
2. 尺寸伸縮
ScaleAnimation(float fromXScale,float toXScale,float fromYScale,float toYScale,int pivotXType,float pivotX,int pivotYType,float pivotY)
fromXScale,toXScale:起始和結束時的X座標的伸縮;
fromYScale,toYScale:起始和結束時的Y座標的伸縮;
pivotX:表示伸縮動畫相對於X座標的開始位置;
pivotY:表示伸縮動畫相對於Y座標的開始位置;
pivotXType:X座標上的伸縮模式;
pivotYType:Y座標上的伸縮模式。
3. 畫面旋轉
RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotX,int pivotYType,float pivotY)
fromDegrees,toDegrees:起始和結束時的角度;
fromYScale,toYScale:起始和結束時的Y座標的伸縮;
pivotX:表示伸縮動畫相對於X座標的開始位置;
pivotY:表示伸縮動畫相對於Y座標的開始位置;
pivotXType:X座標上的伸縮模式;
pivotYType:Y座標上的伸縮模式。
4. 位置移動
TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float toYDelte)
fromXDelta, fromYDelta:起始時候的座標;
toXDelta, toYDelte:結束時候的座標;
當然,在代碼中做了這些設定後,是需要將動畫播放出來的,那麼播放動畫的函數就為:
startAnimation(Animation animation)
animation為要播放的動畫。
同時,需要設定一個播放動畫的時間,利用:
setDuration(long duration)
duration為動畫顯示的時間,以毫秒為單位。
以上介紹的是Android平台下的Tween動畫,Android平台下一共提供了兩種動畫,另外一種就是Frame動畫。
Frame動畫播放,需要有很多幀的圖片。首先還是可以在XML檔案中來說明。
framean.xml:
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/pic1" android:duration="100" /> <item android:drawable="@drawable/pic2" android:duration="100" /> <item android:drawable="@drawable/pic3" android:duration="100" />………</animation-list>
JAVA代碼:
public class GameView extends View{ // 定義AnimationDrawable動畫對象 private AnimationDrawable frameAnimation = null; Context mContext = null; public GameView(Context context) { super(context); mContext = context; //定義一個ImageView用來顯示動畫 ImageView myImageView = new ImageView(mContext); // 裝載動畫布局檔案 myImageView.setBackgroundResource(R.anim.framean); //構建動畫 frameAnimation = (AnimationDrawable)myImageView.getBackground(); //設定是否迴圈 frameAnimation.setOneShot( false ); //設定該類顯示的動畫 this.setBackgroundDrawable(frameAnimation); } public void onDraw(Canvas canvas) { super.onDraw(canvas); } public boolean onKeyUp(int keyCode, KeyEvent event) { switch ( keyCode ) { case KeyEvent.KEYCODE_DPAD_UP: /* 開始播放動畫 */ frameAnimation.start(); break; } return true; }}
直接用代碼,而不用xml描述的方法為:
public class GameView extends View {// 定義AnimationDrawable動畫對象private AnimationDrawableframeAnimation= null;ContextmContext= null;//定義一個Drawable對象Drawable myBitAnimation = null;public GameView(Context context) {super(context);//執行個體化AnimationDrawable對象mContext = context;frameAnimation = new AnimationDrawable();//裝載資源,迴圈裝載需要作為動畫的圖片for(int i=1;i<=3;i++){int id = getResources().getIdentifier("pic"+i, "drawable", mContext.getPackageName());myBitAnimation = getResources().getDrawable(id);//為動畫添加一幀,參數myBitAnimation是該幀的圖片,參數100是顯示時間,毫秒為單位frameAnimation.addFrame(myBitAnimation, 100);}//設定是否迴圈,false表示迴圈frameAnimation.setOneShot(false);//設定本類將要顯示這個動畫this.setBackgroundDrawable(frameAnimation); public void onDraw(Canvas canvas){ super.onDraw(canvas);public boolean onKeyUp(int keyCode, KeyEvent event){switch ( keyCode ){case KeyEvent.KEYCODE_DPAD_UP://開始播放動畫 frameAnimation.start();break;}return true;}}