Android開發(25)--framebyframe幀動畫並實現啟動介面到主介面的跳轉
阿新 • • 發佈:2019-01-26
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);
}
}
效果如下: