動畫類型
Android的animation由四種類型組成
Android動畫模式
Animation主要有兩種動畫模式:
一種是tweened animation(漸變動畫)
XML中
|
JavaCode
|
alpha
|
AlphaAnimation
|
scale
|
ScaleAnimation
|
一種是frame by frame(畫面轉換動畫)
XML中
|
JavaCode
|
translate
|
TranslateAnimation
|
rotate
|
RotateAnimation
|
如何在XML文件中定義動畫
① 打開Eclipse,新建Android工程
② 在res目錄中新建anim文件夾
③ 在anim目錄中新建一個myanim.xml(注意文件名小寫)
④ 加入XML的動畫代碼
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <alpha/>
- <scale/>
- <translate/>
- <rotate/>
- </set>
Android動畫解析--XML
<alpha>
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <alpha
- android:fromAlpha="0.1"
- android:toAlpha="1.0"
- android:duration="3000"
- />
- <!-- 透明度控制動畫效果 alpha
- 浮點型值:
- fromAlpha 屬性爲動畫起始時透明度
- toAlpha 屬性爲動畫結束時透明度
- 說明:
- 0.0表示完全透明
- 1.0表示完全不透明
- 以上值取0.0-1.0之間的float數據類型的數字
-
- 長整型值:
- duration 屬性爲動畫持續時間
- 說明:
- 時間以毫秒爲單位
- -->
- </set>
<scale>
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <scale
- android:interpolator=
- "@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.0"
- android:toXScale="1.4"
- android:fromYScale="0.0"
- android:toYScale="1.4"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillAfter="false"
- android:duration="700" />
- </set>
- <!-- 尺寸伸縮動畫效果 scale
- 屬性:interpolator 指定一個動畫的插入器
- 在我試驗過程中,使用android.res.anim中的資源時候發現
- 有三種動畫插入器:
- accelerate_decelerate_interpolator 加速-減速 動畫插入器
- accelerate_interpolator 加速-動畫插入器
- decelerate_interpolator 減速- 動畫插入器
- 其他的屬於特定的動畫效果
- 浮點型值:
-
- fromXScale 屬性爲動畫起始時 X座標上的伸縮尺寸
- toXScale 屬性爲動畫結束時 X座標上的伸縮尺寸
-
- fromYScale 屬性爲動畫起始時Y座標上的伸縮尺寸
- toYScale 屬性爲動畫結束時Y座標上的伸縮尺寸
-
- 說明:
- 以上四種屬性值
-
- 0.0表示收縮到沒有
- 1.0表示正常無伸縮
- 值小於1.0表示收縮
- 值大於1.0表示放大
-
- pivotX 屬性爲動畫相對於物件的X座標的開始位置
- pivotY 屬性爲動畫相對於物件的Y座標的開始位置
-
- 說明:
- 以上兩個屬性值 從0%-100%中取值
- 50%爲物件的X或Y方向座標上的中點位置
-
- 長整型值:
- duration 屬性爲動畫持續時間
- 說明: 時間以毫秒爲單位
- 布爾型值:
- fillAfter 屬性 當設置爲true ,該動畫轉化在動畫結束後被應用
- -->
<translate>
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="30"
- android:toXDelta="-80"
- android:fromYDelta="30"
- android:toYDelta="300"
- android:duration="2000"
- />
- <!-- translate 位置轉移動畫效果
- 整型值:
- fromXDelta 屬性爲動畫起始時 X座標上的位置
- toXDelta 屬性爲動畫結束時 X座標上的位置
- fromYDelta 屬性爲動畫起始時 Y座標上的位置
- toYDelta 屬性爲動畫結束時 Y座標上的位置
- 注意:
- 沒有指定fromXType toXType fromYType toYType 時候,
- 默認是以自己爲相對參照物
- 長整型值:
- duration 屬性爲動畫持續時間
- 說明: 時間以毫秒爲單位
- -->
- </set>
<rotate>
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <rotate
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromDegrees="0"
- android:toDegrees="+350"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000" />
- <!-- rotate 旋轉動畫效果
- 屬性:interpolator 指定一個動畫的插入器
- 在我試驗過程中,使用android.res.anim中的資源時候發現
- 有三種動畫插入器:
- accelerate_decelerate_interpolator 加速-減速 動畫插入器
- accelerate_interpolator 加速-動畫插入器
- decelerate_interpolator 減速- 動畫插入器
- 其他的屬於特定的動畫效果
-
- 浮點數型值:
- fromDegrees 屬性爲動畫起始時物件的角度
- toDegrees 屬性爲動畫結束時物件旋轉的角度 可以大於360度
-
- 說明:
- 當角度爲負數——表示逆時針旋轉
- 當角度爲正數——表示順時針旋轉
- (負數from——to正數:順時針旋轉)
- (負數from——to負數:逆時針旋轉)
- (正數from——to正數:順時針旋轉)
- (正數from——to負數:逆時針旋轉)
- pivotX 屬性爲動畫相對於物件的X座標的開始位置
- pivotY 屬性爲動畫相對於物件的Y座標的開始位置
-
- 說明: 以上兩個屬性值 從0%-100%中取值
- 50%爲物件的X或Y方向座標上的中點位置
- 長整型值:
- duration 屬性爲動畫持續時間
- 說明: 時間以毫秒爲單位
- -->
- </set>
如何使用XML中的動畫效果
- public static Animation loadAnimation (Context context, int id)
- //第一個參數Context爲程序的上下文
- //第二個參數id爲動畫XML文件的引用
- //例子:
- myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);
- //使用AnimationUtils類的靜態方法loadAnimation()來加載XML中的動畫XML文件
如何在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);
Android動畫解析--JavaCode
AlphaAnimation
① AlphaAnimation類對象定義
- private AlphaAnimation myAnimation_Alpha;
② AlphaAnimation類對象構造
- AlphaAnimation(float fromAlpha, float toAlpha)
- //第一個參數fromAlpha爲 動畫開始時候透明度
- //第二個參數toAlpha爲 動畫結束時候透明度
- myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
- //說明:
- // 0.0表示完全透明
- // 1.0表示完全不透明
③ 設置動畫持續時間
- myAnimation_Alpha.setDuration(5000);
- //設置時間持續時間爲 5000毫秒
ScaleAnimation
① ScaleAnimation類對象定義
- private AlphaAnimation myAnimation_Alpha;
② ScaleAnimation類對象構造
- ScaleAnimation(float fromX, float toX, float fromY, float toY,
- int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
- //第一個參數fromX爲動畫起始時 X座標上的伸縮尺寸
- //第二個參數toX爲動畫結束時 X座標上的伸縮尺寸
- //第三個參數fromY爲動畫起始時Y座標上的伸縮尺寸
- //第四個參數toY爲動畫結束時Y座標上的伸縮尺寸
- /*說明:
- 以上四種屬性值
- 0.0表示收縮到沒有
- 1.0表示正常無伸縮
- 值小於1.0表示收縮
- 值大於1.0表示放大
- */
- //第五個參數pivotXType爲動畫在X軸相對於物件位置類型
- //第六個參數pivotXValue爲動畫相對於物件的X座標的開始位置
- //第七個參數pivotXType爲動畫在Y軸相對於物件位置類型
- //第八個參數pivotYValue爲動畫相對於物件的Y座標的開始位置
- 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_Scale.setDuration(700);
- //設置時間持續時間爲 700毫秒
TranslateAnimation
① TranslateAnimation類對象定義
- private AlphaAnimation myAnimation_Alpha;
② TranslateAnimation類對象構造
- TranslateAnimation(float fromXDelta, float toXDelta,
- float fromYDelta, float toYDelta)
- //第一個參數fromXDelta爲動畫起始時 X座標上的移動位置
- //第二個參數toXDelta爲動畫結束時 X座標上的移動位置
- //第三個參數fromYDelta爲動畫起始時Y座標上的移動位置
- //第四個參數toYDelta爲動畫結束時Y座標上的移動位置
③ 設置動畫持續時間
- myAnimation_Translate.setDuration(2000);
- //設置時間持續時間爲 2000毫秒
RotateAnimation
① RotateAnimation類對象定義
- private AlphaAnimation myAnimation_Alpha;
② RotateAnimation類對象構造
- RotateAnimation(float fromDegrees, float toDegrees,
- int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
- //第一個參數fromDegrees爲動畫起始時的旋轉角度
- //第二個參數toDegrees爲動畫旋轉到的角度
- //第三個參數pivotXType爲動畫在X軸相對於物件位置類型
- //第四個參數pivotXValue爲動畫相對於物件的X座標的開始位置
- //第五個參數pivotXType爲動畫在Y軸相對於物件位置類型
- //第六個參數pivotYValue爲動畫相對於物件的Y座標的開始位置
- myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
- Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
③ 設置動畫持續時間
- myAnimation_Rotate.setDuration(3000);
- //設置時間持續時間爲 3000毫秒
- 新建工程 myFrameAnimation
- 在main.xml佈局中添加view子類,調整一下,效果如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:prientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:prientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:background="@drawable/bt_group_back" android:layout_marginTop="10px">
- <Button android:text="播放動畫" android:layout_width="100px"
- android:id="@+id/Button_start" android:layout_height="fill_parent"></Button>
- <Button android:layout_width="100px" android:text="停止動畫"
- android:id="@+id/Button_stop" android:layout_height="fill_parent"></Button>
- <CheckBox android:text="動畫重複" android:layout_width="100px"
- android:id="@+id/CheckBox_ifCycle_orNot" style="?android:attr/starStyle"
- android:layout_height="fill_parent"></CheckBox>
- </LinearLayout>
- <ImageView android:id="@+id/rocket_image"
- android:layout_width="80px" android:layout_height="80px"
- android:background="@drawable/android_large"
- android:layout_marginLeft="100dp" android:layout_marginTop="100dp"></ImageView>
- </LinearLayout>
- 找幾個動態圖片,把它分成單個圖。(主要是爲了講解/是用FrameAnimation效果,AnimationDrawable)
- 修改mainActivity.java的代碼
-
- package zyf.my.frame.animation;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class myFrameAnimatino extends Activity implements Button.OnClickListener {
- /** Called when the activity is first created. */
- AnimationDrawable frameAnimation;
- /*
- * 聲明AnimationDrawable 可繪製動畫 對象frameAnimation
- */
- ImageView myImage;
- /*
- * 圖片View ImageView
- */
- Button start,stop;
- CheckBox Cycle;
- boolean isChecked_cycle=true;
- /*
- * (non-Javadoc)
- * @see android.app.Activity#onCreate(android.os.Bundle)
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- start=(Button) findViewById(R.id.Button_start);
- stop=(Button) findViewById(R.id.Button_stop);
- Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);
- /*
- * findViewById()從XML中獲取 Button CheckBox
- */
-
- myImage = (ImageView) findViewById(R.id.rocket_image);
- /*
- * findViewById()從XML中獲取ImageView 對象myImage
- */
- myImage.setBackgroundResource(R.anim.myframeanimation);
- /*
- * ImageView.setBackgroundResource()設置 圖片View的背景圖片
- * 這裏是把幀動畫 myframeanimation加到 圖片View的背景中
- */
- frameAnimation = (AnimationDrawable) myImage.getBackground();
- /*
- * myImage.getBackground()獲得背景的Drawable的對象,轉換成AnimationDrawable
- */
-
- start.setOnClickListener(this);
- stop.setOnClickListener(this);
- }
- /*
- * (non-Javadoc)
- * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
- */
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- frameAnimation.setOneShot(isChecked_cycle);
- /*
- * 添加觸摸事件處理方法
- */
- // TODO Auto-generated method stub
- if(event.getAction()==MotionEvent.ACTION_DOWN){
- /*
- * MotionEvent.getAction()獲取事件動作
- * MotionEvent.ACTION_DOWN 向下的手勢動作
- */
- /*event.getAction() 返回正被執行的動作種類:
- * 是 ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一個.
- */
- frameAnimation.start();
- /*
- * 啓動幀動畫效果
- */
- return true;
- }
- return super.onTouchEvent(event);
- }
- /*
- * (non-Javadoc)
- * @see android.view.View.OnClickListener#onClick(android.view.View)
- */
- @Override
- public void onClick(View button) {
- // TODO Auto-generated method stub
- switch (button.getId()) {
- case R.id.Button_start:{
- if(Cycle.isChecked()){
- Toast.makeText(this, "動畫重複", Toast.LENGTH_LONG).show();
- isChecked_cycle=false;
- }else{
- Toast.makeText(this, "不重複", Toast.LENGTH_LONG).show();
- isChecked_cycle=true;
- }
- /*
- * 複選按鈕選中, 動畫重複播放, AnimationDrawable.setOneShot(false)
- * 複選按鈕未選中,動畫不重複播放,AnimationDrawable.setOneShot(true)
- */
-
- frameAnimation.setOneShot(isChecked_cycle);
- /*
- * 設置重複與否
- */
- frameAnimation.start();
- /*
- *啓動幀動畫效果
- */
-
- }
- break;
- case R.id.Button_stop:{
- if(frameAnimation.isRunning()){
- /*
- * AnimationDrawable.isRunning(),判斷幀動畫是否在運行,true---運行中
- * 如果動畫正在運行,可以停止
- */
- frameAnimation.stop();
- /*
- *停止幀動畫效果
- */
- }
-
- }
- break;
- default:
- break;
- }
- }
- }
- package zyf.my.frame.animation;