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();
}
});
}
}