============================首先看看官网上关于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 anAnimationDrawable
.
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>
ELEMENTS:
<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(); // 开始动画
}
}
效果如下:






=================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
分享到:
相关推荐
【作品名称】:基于ffmpeg树莓派实时监控(stearm -> ffmpeg -> nodejs -> websocket -> html) 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期...
在Android开发中,帧动画(Frame Animation)是一种常见的动画效果,它通过连续播放一系列静态图像来模拟动态效果。本文将深入探讨如何在Android项目中应用帧动画,包括使用`AnimationDrawable`和`ImageView`实现这...
Android Animation Detailed Tutorial / Android 动画详尽教程 中文讲解(README)请直接点击对应标题 English explanation(or readme), Do not click on the title, please click on the tip ...
- 在XML文件中,定义`<animation-list>`标签作为根元素,设置`android:oneshot`属性来决定动画是否只播放一次(true为单次,false为循环)。 - 在`<animation-list>`内添加多个`<item>`标签,每个`<item>`代表一帧...
例如,一个简单的帧动画XML可能包含多个`<frame>`元素,每个元素指定一个`android:drawable`(帧的图片)和`android:duration`(帧显示的时间,以毫秒为单位)。 接下来,我们将讨论如何创建和加载帧动画。创建帧...
在Android开发中,动画是提升用户体验的关键因素之一。Android提供了多种动画类型,其中包括 Tween 补间动画和 Frame 逐帧动画。本节我们将深入探讨Frame动画,这是一种将一系列静态图像连续播放以创建动态效果的...
在Android开发中,动画是提升用户体验的关键元素之一。`animation-list`是Android系统提供的一种用于创建帧动画的视图组件,常用来实现如加载、旋转、弹跳等效果。本教程将深入探讨如何使用`animation-list`来创建一...
A-Frame引擎开发:A-Frame动画系统实现_(1).A-Frame基础概念与环境搭建.docx A-Frame引擎开发:A-Frame动画系统实现_(2).A-Frame动画系统的概述.docx A-Frame引擎开发:A-Frame动画系统实现_(3).关键帧动画...
本篇将深入讲解如何利用`Animation-list`在Android中实现逐帧动画。 一、`Animation-list`基础 `Animation-list`是Android XML动画资源的一种类型,它定义了一组子项(通常为ImageView的源),这些子项按照指定的...
A-Frame引擎开发:A-Frame基础入门_(1).A-Frame概述.docx A-Frame引擎开发:A-Frame基础入门_(2).WebVR与A-...A-Frame引擎开发:A-Frame基础入门_(10).A-Frame动画系统.docx A-Frame引擎开发:A-Frame基础入门_
例如,你可以使用`start()`和`stop()`方法来控制动画的播放状态,`seekTo(int frameIndex)`可以定位到指定的帧。 在性能方面,`android-gif-drawable`库采用了优化的解码算法,减少了内存占用,并且能够在多个线程...
帧动画在Android开发中是一种常见的视觉效果,常用于创建简单的动画效果,比如旋转、滑动或者是一些简单的交互动画。本Demo旨在提供一个基础的帧动画实现,以供开发者参考和使用。帧动画的工作原理是通过连续播放一...
帧动画(Frame-by-Frame Animation)是一种在计算机图形学和游戏开发中常用的技术,它通过逐帧播放一系列图像来创建连续的动态效果。在给定的场景中,“点击图片的右边,则图片向右移动,到屏幕边缘停止;点击图片...
描述中提到的"X-Frame-Options头缺失 in a frame because it set 'X-Frame-Options' to 'deny'",意味着在某个特定的场景下,一个网页没有设置X-Frame-Options头,或者设置了值为'deny'的X-Frame-Options头,这表明...
在Android开发中,动画是提升用户体验的关键因素之一。Android提供了多种动画类型,其中包括 Tween 补间动画和 Frame 逐帧动画。本篇文章将主要聚焦于Frame动画,这是一种通过连续显示不同帧图片来创建动画效果的...
帧动画(Frame Animation)是Android提供的一种简单而直观的动画实现方式,适用于实现一系列静态图片按顺序播放的效果,类似于传统的动画胶片。本篇将深入探讨Android帧动画的概念、原理以及如何通过实例进行创建。 ...
-- 动画持续时间,毫秒为单位 --> /> ``` 接下来是缩放动画(scale),它能改变视图的大小。同样地,我们可以在XML文件中定义: ```xml <scale xmlns:android="http://schemas.android.com/apk/res/android" ...
Animation从总体来说可以分为两类: Tweened Animations:该类提供了旋转,移动,...Frame-By-Frame Animations:该类可以创建一个Drawable序列,这些Drawable可以按照指定的事件间隔一个一个显示,和动画片差不多。
同时,Android还提供了补间动画(Tween Animation)和帧动画(Frame Animation),以及属性动画(Property Animation)系统,为开发者提供了更多样化的动画实现方式。 在`animationdemo`项目中,你可能能够找到这些...
2. **Frame-by-Frame Animation (帧动画)**:这种动画模式则是通过快速连续播放一系列不同的图像来模拟运动的效果。适用于复杂的动画场景。 #### 三、XML 文件中定义动画 要在 Android 应用中定义动画,首先需要...