Drawable animation可以加載Drawable資源實現幀動畫。AnimationDrawable是實現Drawable animations的基本類。推薦用XML文件的方法實現Drawable動畫,不推薦在代碼中實現。這種XML文件存放在工程中res/drawable/目錄下。XML文件的指令(即屬性)爲動畫播放的順序和時間間隔。
在XML文件中<animation-list>元素爲根節點,<item>節點定義了每一幀,表示一個drawable資源的幀和幀間隔。此XML文件必須寫在res資源文件目錄下的anim文件夾下,
下面是一個XML文件的實例:
framebyframe.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/m1" android:duration="1000" />
<item android:drawable="@drawable/m2" android:duration="1000" />
<item android:drawable="@drawable/m3" android:duration="1000" />
<item android:drawable="@drawable/m4" android:duration="1000" />
<item android:drawable="@drawable/m5" android:duration="1000" />
<item android:drawable="@drawable/m6" android:duration="1000" />
<item android:drawable="@drawable/m7" android:duration="1000" />
</animation-list>
<!--
oneshot :是否只播放一遍動畫 true 播放一遍 false 循環播放
默認就是false
-->
下面是StartActivity.java
package com.example.lesson18_framebyframe;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.ImageView;
public class StartActivity extends Activity {
private ImageView imageView;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
//幀動畫
imageView = (ImageView) findViewById(R.id.imageView1);
//第一種方式實現的動畫
/*animationDrawable = (AnimationDrawable) getResources().getDrawable(R.anim.framebyframe);
imageView.setBackgroundDrawable(animationDrawable);*/
//第二種方式實現的動畫
imageView.setBackgroundResource(R.anim.framebyframe);
animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==1){
Intent intent = new Intent(StartActivity.this,NextActivity.class);
startActivity(intent);
}
};
}.sendEmptyMessageDelayed(1, 7000);
//animationDrawable.setOneShot(false);是否循環播放
//animationDrawable.stop();停止播放
//animationDrawable.isRunning();//是否播放
//animationDrawable.getNumberOfFrames();//播放幀
//animationDrawable.getFrame(index); 返回制定索引的 Drawable對象
//animationDrawable.getDuration(i);停留的時間
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
}
佈局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".StartActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true" />
</RelativeLayout>
NextActivity.java
package com.example.lesson18_framebyframe;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NextActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("啓動界面到主界面完成");
setContentView(tv);
}
}
效果如下: