android中動畫分爲3種:html
下面只介紹前兩種動畫的使用方法,屬性動畫將在後續文章中介紹java
一 Tween Animationandroid
Tween Animation有四種形式:動畫
l alpha 漸變透明度動畫效果spa
l scale 漸變尺寸伸縮動畫效果code
l translate 畫面位置移動動畫效果xml
l rotate 畫面旋轉動畫效果htm
這四種動畫實現方式都是經過Animation類和AnimationUtils配合實現。對象
能夠經過xml實現:動畫的XML文件在工程中res/anim目錄。接口
例如:rotate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter = "false" android:zAdjustment="bottom" > <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="4000" /> </set>
使用動畫
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.rotate); //監聽動畫的狀態(開始,結束) anim.setAnimationListener(new EffectAnimationListener()); textWidget = (TextView)findViewById(R.id.text_widget); textWidget.setText("畫面旋轉動畫效果"); textWidget.startAnimation(anim);
二 Frame Animation
Frame Animation是順序播放事先作好的圖像,跟電影相似。不一樣於animation package,Android SDK提供了另一個類AnimationDrawable來定義使用Frame Animation。
利用xml文件實現:res/drawable-hdpi/frame.xml:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > <item android:drawable="@drawable/p1" android:duration="1000"></item> <item android:drawable="@drawable/p2" android:duration="1000"></item> <item android:drawable="@drawable/p3" android:duration="1000"></item> <item android:drawable="@drawable/p4" android:duration="1000"></item> <item android:drawable="@drawable/p5" android:duration="1000"></item> <item android:drawable="@drawable/p6" android:duration="1000"></item> </animation-list>使用動畫
AnimationDrawable anim = (AnimationDrawable)getResources(). getDrawable(R.drawable.frame); textWidget = (TextView)findViewById(R.id.text_widget); textWidget.setText("背景漸變更畫效果"); textWidget.setBackgroundDrawable(anim); anim.start();
這裏有點不一樣的是,利用AnimationDrawable實現動畫時,自己並無提供接口來監聽動畫的狀態(開始,結束),須要本身處理。