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 ...
随机推荐
- 按钮/文本框 disabled
需求:已登入的,将用户的信息回填,用户文本框内容不能更改. 按钮: <button type="button" id="btnSearch">< ...
- Tomcat发布项目时,浏览器地址栏图标的问题
最近在做一个Java网络应用程序,服务器是tomcat.在默认情况下,当用户访问该网络应用时,地址栏图标显示为tomcat猫.我希望把它换成自己的图标,于是研究了一下.在研究过程中,我发现网上的资料大 ...
- ORA-30036
http://blog.sina.com.cn/s/blog_676255e101018d5s.html
- 创建一个spring helloworld
1.下载所需要的jar包 http://projects.spring.io/spring-framework/ 这里使用了maven方式给出jar <dependencies> < ...
- webstrom插件:如何设置才能让webstrom能提示bootstrap的语法
<link href="bootstrap.min.css" rel="stylesheet"> <link href="boots ...
- oracle 11g 服务端下载地址及安装说明
oracle 11g 服务端下载地址及安装说明 分类: Oracle 2013-11-17 19:40 988人阅读 ...
- ProGuard
ProGuard的作用: 1.创建紧凑的代码文档是为了更快的网络传输,快速装载和更小的内存占用. 2.创建的程序和程序库很难使用反向工程. 3.所以它能删除来自源文件中的没有调用的代码 4.充分利用 ...
- JQuery基础教程:入门
JQuery能做什么 JQuery在线手册 1.取得文档中的元素 如果不使用JavaScript库,遍历DOM(Document Object Model,文档对象模型)树,以及查找HTML文档结构中 ...
- C#中char[]与string之间的转换
string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string st ...
- Android Studio实用快捷键汇总
以下是平时在Windwos系统上用Android Studio进行开发时常用到的一些快捷键,虽然不多,但是感觉都还蛮实用的,因此记录下来,如果什么时候不小心忘记了可以拿来翻一翻,That would ...