Android --- Frame动画示例

来源:互联网 发布:淘宝删除的购物车 编辑:程序博客网 时间:2024/05/17 03:53

Frame动画:

1、找到一组图片c01.jpg,c02.jpg,c03.jpg,c04.jpg,c05.jpg,copy到res/drawable目录下;

2、在res/drawable目录下新建XML文件:frame_anim.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/c01" android:duration="500"/> <item android:drawable="@drawable/c02" android:duration="500"/> <item android:drawable="@drawable/c03" android:duration="500"/> <item android:drawable="@drawable/c04" android:duration="500"/> <item android:drawable="@drawable/c05" android:duration="500"/></animation-list>
3、在res/layout目录下新建XML文件:frame_anim_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="centerInside"android:id="@+id/imgFrame"android:background="@drawable/frame_anim"></ImageView><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:text="开始"android:id="@+id/btnStart"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1.0"></Button><Buttonandroid:text="结束"android:id="@+id/btnEnd"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1.0"></Button></LinearLayout></LinearLayout>
4、Activity文件中的代码:

package com.bison;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 AnimationDemoActivity extends Activity implements OnClickListener {// 开始按钮private Button btnStart;// 结束按钮private Button btnEnd;private ImageView imgFrame;// 声明Frame动画对象private AnimationDrawable frameAnim;/** 初始化 */public void init() {btnStart = (Button) findViewById(R.id.btnStart);btnEnd = (Button) findViewById(R.id.btnEnd);btnStart.setOnClickListener(this);btnEnd.setOnClickListener(this);imgFrame = (ImageView) findViewById(R.id.imgFrame);// 将ImageView的backgroud声明给Frame动画对象frameAnim = (AnimationDrawable) imgFrame.getBackground();}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 加载layout.frame_anim_layout页面setContentView(R.layout.frame_anim_layout);init();}public void onClick(View v) {// 判断按钮事件switch (v.getId()) {case R.id.btnStart:frameAnim.start();break;case R.id.btnEnd:frameAnim.stop();break;}}}

PS:Frame动画原理类似于电影胶片,一幕一幕的闪过,在人的视觉停留期快速变动,形成组图,产生动画。