在XML中定义逐帧动画
来源:互联网 发布:全局搜索能力强的算法 编辑:程序博客网 时间:2024/05/17 03:19
逐帧(Frame)是最容易理解的动画,它要求开发者把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,然后利用人眼“视觉暂留”原理,给用户造成“动画”的错觉。逐帧动画的动画原理与放电影的原理一样。
下面使用在XML中定义逐帧动画的每一帧,代码如下:
Activity:
package com.lovo.frameanim;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 MainActivity extends Activity implements OnClickListener {private Button startBtn;private Button stopBtn;private ImageView image;// 声明AnimationDrawable对象private AnimationDrawable animationDrawable;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startBtn = (Button) findViewById(R.id.main_btn_start);stopBtn = (Button) findViewById(R.id.main_btn_stop);startBtn.setOnClickListener(this);stopBtn.setOnClickListener(this);image = (ImageView) findViewById(R.id.main_image);// 从image中获取AnimationDrawable对象animationDrawable = (AnimationDrawable) image.getBackground();}@Overridepublic void onClick(View v) {if (v == startBtn) {// 启动动画animationDrawable.start();}if (v == stopBtn) {// 停止动画animationDrawable.stop();}}}
动画XML(freedom_frame_anim.xml):
<?xml version="1.0" encoding="utf-8"?><!-- android:oneshot:是否循环;true-不循环, false-循环 --><animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/freedom1" android:duration="50"></item> <item android:drawable="@drawable/freedom2" android:duration="50"></item> <item android:drawable="@drawable/freedom3" android:duration="50"></item> <item android:drawable="@drawable/freedom4" android:duration="50"></item> <item android:drawable="@drawable/freedom5" android:duration="50"></item> <item android:drawable="@drawable/freedom6" android:duration="50"></item> <item android:drawable="@drawable/freedom7" android:duration="50"></item> <item android:drawable="@drawable/freedom8" android:duration="50"></item></animation-list>
Activity布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/main_btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始动画" /> <Button android:id="@+id/main_btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止动画" /> <ImageView android:id="@+id/main_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/freedom_frame_anim" /></LinearLayout>
- 在XML中定义逐帧动画
- Android 如何在XML文件中定义动画
- Android 如何在XML文件中定义动画
- 使用定义在xml下的动画
- 【实训】Android中xml动画定义
- 安卓开发中如何在XML文件中定义动画
- 在XML中定义菜单
- 在XML中定义菜单
- android动画详解六 XML中定义动画
- 在Style中定义动画类型
- 使用XML定义动画
- 安卓XML和java文件中定义基本动画
- Android 在XML中定义菜单
- 在strings.xml中定义html标签
- 在strings.xml中定义html标签
- 在web.xml中配置servlet定义
- 使用Drawable资源--在xml中定义
- 在xml文件中定义图片数组
- ExcelXML
- 数学之美系列六:图论和网络爬虫 (Web Crawlers)
- LeetCode-Palindrome Number
- libavformat
- 关于分页的一次失败的尝试
- 在XML中定义逐帧动画
- UVa 10916 - Factstone Benchmark
- jquery Temp jsonToAction
- Android心得体会 -- Activity改写成类似Dialog实现弹出式菜单
- 如何在zencart里实现多标签标签云效果
- 1366 Incorrect integer value
- 相机防抖
- ubuntu 安装nginx
- 快速Linux重装XP系统