Android动画之逐帧动画(FrameAnimation)详解
今天我们就来学习逐帧动画,废话少说直接上效果图如下:

帧动画的实现方式有两种:
一、在res/drawable文件夹下新建animation-list的XML实现帧动画
1、首先在res/drawable文件夹下添加img00-img24共25张图片
2、新建frame_anim.xml
- <?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true" >
- <!-- animation-list 帧动画 -->
- <!-- android:oneshot的值为 false代表播放多次,true代表只播放一次 -->
- <!-- duration代表每张图片的播放时间 ,定义一个持续时间为50毫秒的动画帧 -->
- <item
- android:drawable="@drawable/img00"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img01"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img02"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img03"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img04"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img05"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img06"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img07"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img08"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img09"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img10"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img11"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img12"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img13"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img14"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img15"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img16"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img17"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img18"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img19"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img20"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img21"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img22"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img23"
- android:duration="50"/>
- <item
- android:drawable="@drawable/img24"
- android:duration="50"/>
- </animation-list>
3、在activity_main中添加控件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.havorld.frameanimation.MainActivity" >
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true" />
- <!-- android:background="@drawable/frame_anim" -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:orientation="horizontal"
- android:padding="10dp" >
- <Button
- android:id="@+id/start"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="播放" />
- <Button
- android:id="@+id/stop"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="停止" />
- </LinearLayout>
- </RelativeLayout>
4、在代码中获取并开启帧动画
- public class MainActivity extends Activity implements OnClickListener {
- private ImageView imageView;
- private AnimationDrawable animationDrawable;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.imageView);
- findViewById(R.id.start).setOnClickListener(this);
- findViewById(R.id.stop).setOnClickListener(this);
- setXml2FrameAnim1();
- // setXml2FrameAnim2();
- }
- /**
- * 通过XML添加帧动画方法一
- */
- private void setXml2FrameAnim1() {
- // 把动画资源设置为imageView的背景,也可直接在XML里面设置
- imageView.setBackgroundResource(R.drawable.frame_anim);
- animationDrawable = (AnimationDrawable) imageView.getBackground();
- }
- /**
- * 通过XML添加帧动画方法二
- */
- private void setXml2FrameAnim2() {
- // 通过逐帧动画的资源文件获得AnimationDrawable示例
- animationDrawable = (AnimationDrawable) getResources().getDrawable(
- R.drawable.frame_anim);
- imageView.setBackground(animationDrawable);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.start:
- if (animationDrawable != null && !animationDrawable.isRunning()) {
- animationDrawable.start();
- }
- break;
- case R.id.stop:
- if (animationDrawable != null && animationDrawable.isRunning()) {
- animationDrawable.stop();
- }
- break;
- default:
- break;
- }
- }
- }
二、通过代码实现帧动画
- /**
- * 通过代码添加帧动画方法
- */
- private void setSrc2FrameAnim() {
- animationDrawable = new AnimationDrawable();
- // 为AnimationDrawable添加动画帧
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img00), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img01), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img02), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img03), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img04), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img05), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img06), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img07), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img08), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img09), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img10), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img11), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img12), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img13), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img14), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img15), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img16), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img17), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img18), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img19), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img20), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img21), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img22), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img23), 50);
- animationDrawable.addFrame(
- getResources().getDrawable(R.drawable.img24), 50);
- // 设置为循环播放
- animationDrawable.setOneShot(false);
- imageView.setBackground(animationDrawable);
- }
Android动画之逐帧动画(FrameAnimation)详解的更多相关文章
- Android 学习之逐帧动画(Frame)
帧动画就是将一些列图片.依次播放. 利用肉眼的"视觉暂留"的原理,给用户的感觉是动画的错觉,逐帧动画的原理和早期的电影原理是一样的. a:须要定义逐帧动画,能够通过代码定义.也能够 ...
- android中的逐帧动画
在android中实现动画最简单的一种方式就是使用逐帧动画(AnimationDrawable).逐帧动画的原理同最古老的动画机制是一样的,通过快速的播放一组变化微小的图片,在人眼的视差时间下,达到一 ...
- Android(java)学习笔记198:Android下的逐帧动画(Drawable Animation)
1.帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一帧 ...
- Android(java)学习笔记141:Android下的逐帧动画(Drawable Animation)
1. 帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一 ...
- Android简单逐帧动画Frame的实现(三)
android之动画(三)通过AnimationDrawable控制逐帧动画 android与逐帧动画: 效果图: 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态. ...
- Android中实现一个简单的逐帧动画(附代码下载)
场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...
- css3 实现逐帧动画
css3 实现逐帧动画 实现逐帧动画需要使用到的是Animation动画,该CSS3的Animation有八个属性:分别是如下:1: animation-name2: animation-durati ...
- Android中的动画详解系列【1】——逐帧动画
逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...
- Android 逐帧动画isRunning 一直返回true的问题
AnimationDrawabl主要通过xml实现逐帧动画,SDK实例如下: An AnimationDrawable defined in XML consists of a single < ...
随机推荐
- Android开发属性动画
普通动画效果和属性动画效果区别: 普通动画效果的动画播放后只是产生了视觉欺骗,并没有移动真实的控件. 属性动画直接真实的移动控件 AnimationSet动画: TextView t1 = (Text ...
- Elasticsearch5.5 多机集群配置和x-pack安装配置
x-pack安装配置 https://www.elastic.co/guide/en/elasticsearch/reference/current/installing-xpack-es.html ...
- Python Machine Learning-Chapter3
Chapter 3:A Tour of Machine Learning Classifiers Using Scikit-learn 3.1:Training a perceptron via sc ...
- 私有仓库GitLab快速入门篇
私有仓库GitLab快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 安装文档请参考官网:https://about.gitlab.com/installation/#ce ...
- 面向对象【day08】:反射(五)
本节内容 概述 反射函数 综合使用 一.概述 反射我们以后会经常用到,这个东西实现了动态的装配,通过字符串来反射类中的属性和方法 二.反射函数 2.1 hasarttr(obj,name_str) 作 ...
- 设计模式---对象创建模式之抽象工厂模式(Abstract Factory)
一:概念 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象 二:动机 在软件系统 ...
- socket技术详解(看清socket编程)
socket编程是网络常用的编程,我们通过在网络中创建socket关键字来实现网络间的通信,通过收集大量的资料,通过这一章节,充分的了解socket编程,文章用引用了大量大神的分析,加上自己的理解,做 ...
- CM记录-选择合适的硬件
hadoop的运行环境---hadoop/jvm/os/hw 原则1:主节点的可靠性要好于从节点:NameNode(Yarn-ResourceManager).DataNode(NodeManager ...
- Study 7 —— while循环中止语句
循环的终止语句break #用于完全结束一个循环,跳出循环体执行循环后面的语句continue #只终止本次循环,接着执行后面的循环 1. 打印0-100,截止到第6次 count = 0 while ...
- 为什么推荐InnoDB引擎使用自增主键?
索引使用时遇到的问题(顺丰)--InnoDB引擎不使用自增主键导致性能问题,也可答最左前缀 InnoDB自增主键 InnoDB主索引(同时也是数据文件)的示意图: 上文讨论过InnoDB的索引实现,I ...