Android开发教程AnimationDrawable逐帧播放动画
下面我们一起来看篇Android开发AnimationDrawable控制逐帧播放动画实现过程,希望文章对各位朋友带不一些帮助。
当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态。
activity代码:
代码如下
package cn.com.chenzheng_java.animation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
/**
* @description android中的逐帧动画.
* 逐帧动画的原理很简单,跟电影的播放一样,一张张类似的图片不停的切换,当切换速度达到一定值时.
* 我们的视觉就会出现残影,残影的出现保证了视觉上变化的连续性,这时候图片的切换看在我们眼中就跟真实的一样了。
* 想使用逐帧动画:
* 第一步:需要在res/drawable文件夹下新建一个xml文件,该文件详细定义了动画播放时所用的图片、切换每张图片
* 所用的时间、是否为连续播放等等。(有些文章说,在res/anim文件夹下放置该文件,事实证明,会出错哦)
* 第二步:在代码中,将该动画布局文件,赋值给特定的图片展示控件,如本例子中的ImageView。
* 第三步:通过imageView.getBackGround()获取相应的AnimationDrawable对象,然后通过该对象的方法进行控制动画
* @author chenzheng_java
*
*/
public class Animation1Activity extends Activity {
ImageView imageView ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation1);
imageView = (ImageView) findViewById(R.id.imageView_animation1);
imageView.setBackgroundResource(R.drawable.animation1_drawable);
}
public void myClickHandler(View targetButton){
// 获取AnimationDrawable对象
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
// 动画是否正在运行
if(animationDrawable.isRunning()){
//停止动画播放
animationDrawable.stop();
}
else{
//开始或者继续动画播放
animationDrawable.start();
}
}
}
animation1.xml文件:
代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.maiziedu.com"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<Button android:id="@+id/button_animation1" android:text="动画开始"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>
<ImageView android:id="@+id/imageView_animation1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"></ImageView>
</LinearLayout>
存放动画文件的xml文件:
代码如下
<?xml version="1.0" encoding="utf-8"?>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item android:drawable="@drawable/a1" android:duration="50"></item>
<item android:drawable="@drawable/a2" android:duration="50"></item>
<item android:drawable="@drawable/a3" android:duration="50"></item>
<item android:drawable="@drawable/a4" android:duration="50"></item>
<item android:drawable="@drawable/a5" android:duration="50"></item>
<item android:drawable="@drawable/a6" android:duration="50"></item>
</animation-list>
除此之外:在AnimationDrawable中,我们还可以在IT在线教育平台麦子学院找到如下的几个重要方法:
setOneShot(boolean flag) 和在配置文件中进行配置一样,可以设置动画是否播放一次,false为连续播放;
addFrame (Drawable frame, int duration) 动态的添加一个图片进入该动画中.
Android开发教程AnimationDrawable逐帧播放动画的更多相关文章
- 转Android 用Animation-list实现逐帧动画
Android 用Animation-list实现逐帧动画 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/arti ...
- 【Android开发VR实战】二.播放360°全景视频
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53924006 本文出自[DylanAndroid的博客] [Android开发 ...
- Android开发教程大全介绍
Android是由谷歌在2007年推出的一个开放系统平台,主要针对移动设备市场,目前版本为Android 4.0.Android基于Linux,开发者可以使用Java或C/C++开发Android应用 ...
- ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView
原文地址: ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http:/ ...
- ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map
原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NE ...
- ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置
原文地址: ArcGIS Runtime for Android开发教程V2.0(2)开发环境配置 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.c ...
- ArcGIS Runtime for Android开发教程V2.0(1)基本概念
原文地址: ArcGIS Runtime for Android开发教程V2.0(1)基本概念 - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http://blog.csd ...
- 【Android 开发教程】动态添加Fragments
本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...
- 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。
AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...
随机推荐
- Struts2 Annotation 注解配置
也叫Zero Configuration(零配置),它省去了写xml文件的麻烦,可以直接在类叫进行配置,不用在java文件和xml文件中来回切换. 必须导入struts2-convention-plu ...
- memcached客户端的使用
一. 概念 Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能. 二. 适用场合 1. 分布式应用 ...
- linux下安装nginx、pcre、zlib、openssl
1.安装nginx之前需要安装PCRE库的安装 最新下载地址 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ tar –zxvf p ...
- [ActionScript 3.0] flash如何访问父级或者舞台上的变量、函数等的方法
方法一: 进行类型转换,先将root.parent强制转换为MovieClip类型,再进行使用,如下:MovieClip(root).i.MovieClip(this.parent).i.MovieC ...
- jquery ui autocomplete combox格式设置
<style> .custom-combobox {//设置输入框格式 position: relative; display: inline-block; width: 62%; } . ...
- PHP 连接 MSSQL
1 设置 2 php 代码: <?php header('Content-Type:text/html; charset=GBK'); define('DB_HOST','localhost') ...
- C++ DLL中导出函数的声明的方法
定义: TESTDLLEXPORT_API int fnTestDllExport(void); TESTDLLEXPORT_API int fnTestCall(void); TESTDLLEXPO ...
- nyoj 93 汉诺塔(三)
点击打开链接 汉诺塔(三) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝 ...
- [ZOJ 1010] Area (计算几何)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1010 题目大意:给你n个点,问你顺次连线能否连成多边形?如果能, ...
- iOS 模态视图
模态视图不是专门的某个类,而是通过视图控制器的presentViewController方法弹出的视图,我们称为模态视图. 模态视图出现的场景一般是临时弹出的窗口,譬如:登录窗口: 模态视图弹出时通过 ...