我的Android进阶之旅------>Android之动画之Frame Animation实例
============================首先看看官网上关于Frame animation的介绍================================
地址:http://developer.android.com/guide/topics/resources/animation-resource.html#Frame
Frame animation
An animation defined in XML that shows a sequence of images in order (like a film).
- FILE LOCATION:
res/drawable/filename.xml
The filename will be used as the resource ID.- COMPILED RESOURCE DATATYPE:
- Resource pointer to an
AnimationDrawable. - RESOURCE REFERENCE:
- In Java:
R.drawable.filename
In XML:@[package:]drawable.filename - SYNTAX:
-
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />
</animation-list>
<animation-list>- Required. This must be the root element. Contains one or more
<item>elements.attributes:
android:oneshot- Boolean. "true" if you want to perform the animation once; "false" to loop the animation.
<item>- A single frame of animation. Must be a child of a
<animation-list>element.attributes:
android:drawable- Drawable resource. The drawable to use for this frame.
android:duration- Integer. The duration to show this frame, in milliseconds.
============================下面通过一个小案例来学习Frame animation================================
step1:新建一个Android项目FrameAnimationDemo
step2:准备好该应用使用的图片,用来做Frame Animation的,并将动画放入drawable目录下
step3:新建一个用来描述Frame动画的xml文件,res/anim/frame.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/p1" android:duration="500" />
<item android:drawable="@drawable/p2" android:duration="500" />
<item android:drawable="@drawable/p3" android:duration="500" />
<item android:drawable="@drawable/p4" android:duration="500" />
<item android:drawable="@drawable/p5" android:duration="500" />
<item android:drawable="@drawable/p6" android:duration="500" />
</animation-list> <!-- android:oneshot指示是否只运行一次,设置为false则意味着循环播放
<item>元素代表一帧动画,
android:drawable指定此帧动画所对应的图片资源,
android:druation代表此帧持续的时间,整数,单位为毫秒。
-->
step4:该应用的布局文件 res/layout/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"> <!-- Frame动画图片 -->
<ImageView android:id="@+id/ImgDance" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />
<!-- android:layout_weight="1" 不设置该属性,下面的两个按钮会被覆盖不显示出来 --> <!-- 动画控制按钮 -->
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="开始跳舞"
android:onClick="runFrame" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="结束跳舞"
android:onClick="stopFrame" />
</LinearLayout>
step5:该应用的主文件,FrameActivity.java
package cn.oyp.frame; import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; public class FrameActivity extends Activity {
// 显示动画的组件
private ImageView imgDance;
// Frame动画
private AnimationDrawable animDance; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化组件
imgDance = (ImageView) super.findViewById(R.id.ImgDance);
} /**
* 如果在onCreate()中调用AnimationDrawable的start()方法,则它只停留在第一帧,并没有出现我们期望的动画,
* 这是因为窗口Window对象还没有完全初始化,AnimationDrawable不能完全追加到窗口Window对象中。
* 而onWindowFocusChanged是在onCreate之后被调用的,当Activity展示给用户时,onWindowFocusChanged方法就会被调用,
* 所以在这儿调用AnimationDrawable的start()方法可以实现动画效果。
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// 将动画资源文件res/anim/frame.xml设置为ImageView的背景
imgDance.setBackgroundResource(R.anim.frame);
// 获取ImageView背景,此时已被编译成AnimationDrawable
animDance = (AnimationDrawable) imgDance.getBackground();
animDance.start();
} /**
* 按钮:停止‘跳舞’动画
*/
public void stopFrame(View view) {
animDance = (AnimationDrawable) imgDance.getBackground();
if (animDance.isRunning()) { // 如果正在运行,就停止
animDance.stop();
}
} /**
* 按钮:开始‘跳舞’动画
*/
public void runFrame(View view) {
// 完全编码实现的动画效果
animDance = new AnimationDrawable();
for (int i = 1; i <= 6; i++) {
// 根据资源名称和目录获取R.java中对应的资源ID
int id = getResources().getIdentifier("p" + i, "drawable",
getPackageName());
// 根据资源ID获取到Drawable对象
Drawable drawable = getResources().getDrawable(id);
// 将此帧添加到AnimationDrawable中
animDance.addFrame(drawable, 500);
}
animDance.setOneShot(false); // 设置为loop
imgDance.setBackgroundDrawable(animDance); // 将动画设置为ImageView背景
animDance.start(); // 开始动画
} }
效果如下:
==================================下面看一个gif动画===========================================
=================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
我的Android进阶之旅------>Android之动画之Frame Animation实例的更多相关文章
- 我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法
我的Android进阶之旅-->Android颜色值(RGB)所支持的四种常见形式 透明度百分比和十六进制对应关系表格 透明度 十六进制 100% FF 99% FC 98% FA 97% F7 ...
- 我的Android进阶之旅------>Android中查看应用签名信息
一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...
- 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计
要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...
- 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现
我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)
在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)
正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piec ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)
对于游戏玩家而言,游戏界面上看到的"元素"千变万化:但是对于游戏开发者而言,游戏界面上的元素在底层都是一些数据,不同数据所绘制的图片有所差异而已.因此建立游戏的状态数据模型是实现游 ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之开发游戏界面(二)
连连看的游戏界面十分简单,大致可以分为两个区域: 游戏主界面区 控制按钮和数据显示区 1.开发界面布局 本程序使用一个RelativeLayout作为整体的界面布局元素,界面布局上面是一个自定义组件, ...
随机推荐
- EffectiveJava(14)在公有类中使用访问方法而非公有域
1.公有类永远都不应该暴露可变的域.如果域是不可变的,暴露公有类的危害就要小一些. 但是,有时候需要用包级私有的或者私有的嵌套类来暴露域,无论这个类是否可变 2.如果公有类暴露了它的访问域,要想在将来 ...
- AcpectJ注释方式配置AOP
1.AspectJ的概念 @AspectJ类似于Java注解的普通Java类 Spring可以使用AspectJ来做切入点解析 AOP的运行时仍旧是纯的Spring AOP,对Aspect ...
- 函数传参,改变Div任意属性的值&&图片列表:鼠标移入/移出改变图片透明度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Win7系统管理员设置了系统策略,禁止进行此安装,怎么办
系统管理员设置了系统策略,禁止进行此安装,怎么办 最佳答案 尝试方法一: windows开始菜单,运行里面输入gpedit.msc打开组策略, 在"计算机配置"→管理模板→ ...
- TCP/IP 网络编程(五)
优于 select 的 epoll (I/O 复用) select 速度慢的原因 调用select后针对全部文件描写叙述符的循环 每次调用函数时都须要向该函数传递监视对象信息 select并非把发生变 ...
- java web 分页实现
分页实现的效果: ///////// /////////////////////////////////////////////////////// /////////////////// ...
- Excel如何取消显示分页虚线
点击普通模式,保存文件,关闭文件再打开就好了. 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有问题一起学习欢迎留言.评论.
- Ajax系列之中的一个:ajax旧貌换新颜
什么是ajax? 什么是Ajax? Ajax就是Asynchronous +JavaScript+XML.中文翻译为:异步的javascript与XML,它是利用javascript语言和xml数据实 ...
- 调用获取学生信息的接口,保存到excel里面的小程序
# 2.http: // doc.nnzhp.cn / index.php?s = / 6 & page_id = 14# 调用获取学生信息的接口,保存到excel里面 import requ ...
- 中小企业 DevOps 从 0 到 1
原文:http://www.sohu.com/a/145065274_262549 今天主要有四个课题: 先聊一聊 DevOps: 然后跟大家聊一聊运维知识的体系和职业发展: 再是中小企业基于开源的 ...