使用逐帧动画实现一个忐忑的精灵动画

来源:互联网 发布:ai for mac中文版 编辑:程序博客网 时间:2024/04/28 09:28

1、在res目录下,创建一个名称为anim的目录,在该目录下添加一个名称为fairy.xml的资源文件

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/img001" android:duration="60"/><item android:drawable="@drawable/img002" android:duration="60"/><item android:drawable="@drawable/img003" android:duration="60"/>    <item android:drawable="@drawable/img004" android:duration="60"/><item android:drawable="@drawable/img005" android:duration="60"/><item android:drawable="@drawable/img006" android:duration="60"/>   </animation-list>

2、布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@anim/fairy"    android:id="@+id/ll"    android:orientation="vertical" ></LinearLayout>

3、MainActivity

public class MainActivity extends Activity {private boolean flag = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);LinearLayout ll = (LinearLayout) findViewById(R.id.ll);// 获取布局文件中添加的线性布局管理器final AnimationDrawable anim = (AnimationDrawable) ll.getBackground();// 获取AnimationDrawable对象// 为线性布局管理器添加单击事件监听器ll.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (flag) {anim.start();// 开始播放动画flag = false;} else {anim.stop();// 停止播放动画flag = true;}}});}}


0 0
原创粉丝点击