我的Android进阶之旅------>Android之动画之Frame Animation实例

简介: ============================首先看看官网上关于Frame animation的介绍================================ 地址:http://developer.


============================首先看看官网上关于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>
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(); // 开始动画
	}

}

效果如下: 


                  

                


==================================下面看一个gif动画===========================================

                          

=================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================


相关文章
|
4月前
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
214 30
|
4月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
432 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
4月前
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
144 3
|
4月前
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
271 65
|
4月前
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
127 26
|
4月前
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
Android自定义view之围棋动画(化繁为简)
|
4月前
|
Java Android开发 开发者
Android自定义view之围棋动画
本文详细介绍了在Android中自定义View实现围棋动画的过程。从测量宽高、绘制棋盘背景,到创建固定棋子及动态棋子,最后通过属性动画实现棋子的移动效果。文章还讲解了如何通过自定义属性调整棋子和棋盘的颜色及动画时长,并优化视觉效果,如添加渐变色让白子更明显。最终效果既可作为围棋动画展示,也可用作加载等待动画。代码完整,适合进阶开发者学习参考。
|
移动开发 前端开发 Java
Android 进阶路线(思维导图)
Android 进阶路线(思维导图)
Android 进阶路线(思维导图)
|
6天前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
91 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
16天前
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
68 6

热门文章

最新文章