Android圖形與圖像處理-逐幀動畫
實例:功夫熊貓
創建項目:FatPo
項目運行效果:
項目截圖:
布局文件:main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#fff"
- >
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- >
- <Button
- android:id="@+id/play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/play"/>
- <Button
- android:id="@+id/stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/stop"/>
- </LinearLayout>
- <ImageView
- android:id="@+id/anim"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@anim/fat_po"
- android:scaleType="center"
- />
- </LinearLayout>
動畫資源資源文件:res\anim\fat_po.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/fat_po_f01" android:duration="60"/>
- <item android:drawable="@drawable/fat_po_f02" android:duration="60"/>
- <item android:drawable="@drawable/fat_po_f03" android:duration="60"/>
- <item android:drawable="@drawable/fat_po_f04" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f05" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f06" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f07" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f08" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f09" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f10" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f11" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f12" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f13" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f14" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f15" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f16" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f17" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f18" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f19" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f20" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f21" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f22" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f23" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f24" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f25" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f26" android:duration="60" />
- <item android:drawable="@drawable/fat_po_f27" android:duration="60" />
- </animation-list>
Activity文件:FatPo.java
- package wwj.fatpo;
-
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
-
- public class FatPo extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //獲取兩個按鈕
- Button play = (Button) findViewById(R.id.play);
- Button stop = (Button) findViewById(R.id.stop);
- ImageView imageView = (ImageView)findViewById(R.id.anim);
- //獲取AnimationDrawable動畫對象
- final AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();
- play.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //開始播放動畫
- anim.start();
- }
- });
- stop.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //停止播放動畫
- anim.stop();
- }
- });
- }
- }