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 < ...
随机推荐
- 修改select下拉选的默认选中值
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- windows 下mysql 重设root密码方法
1.首先在命令行里关闭mysql服务 net stop mysql2.在命令行里进入mysql安装目录下bin目录,以不检查权限的方式启动:mysqld-nt --skip-grant-tables ...
- java代码示例(6-2)
创建Customer.java /*** * 需求分析:创建客户类 * @author chenyanlong * 日期:2017/10/15 */ package com.hp.test06; pu ...
- 4、JDBC-API
访问数据库 /** * 在 java.sql 包中有 3 个接口分别定义了对数据库的调用的不同方式: * * Statement * * PrepatedStatement * * CallableS ...
- Hbase记录-Hbase shell使用命令
1.进入hbase shell 执行./bin/hbase shell 2.进入后,help 帮助信息,如可以使用help 'create' 3.创建表:create 'test','cf' 表 ...
- python -- 进程线程协程专题
进程专栏 multiprocessing 高级模块 要让Python程序实现多进程(multiprocessing),我们先了解操作系统的相关知识. Unix/Linux操作系统提供了一个fork() ...
- Git 设置过滤忽略的文件或文件夹(ignore file)
我们一般向代码仓库提交项目的时候,一般需要忽略编译生成的中间文件以及文件夹的提交,因为它们是无用的,而且也会占用仓库的空间.一般只用提交.pro,.sln,makefile,程序源文件等编译必须用到的 ...
- 如何利用FastReport.Net 设计并导出报表?
在你的程序中,你可以使用FastReport.Net 报表设计器.要做到这一点,你需要使用报表对象设计方法: report1 = new Report();report1.Load("rep ...
- TCP/IP详解 卷1 第十七章 TCP:传输控制协议
17.2 TCP的服务 TCP提供了一种面向连接的.可靠的字节流服务.两个使用TCP的应用在彼此交换数据之前必须先建立一个TCP连接. TCP通过下列方式来提供可靠性: 1) 应用数据被分割成TCP ...
- mysql 原理~ 乐观锁和悲观锁
一 简介:今天咱们来聊聊悲观锁和乐观锁 二 悲观锁 1 定义 在关系数据库管理系统里,悲观并发控制(又名“悲观锁”,Pessimistic Concurrency Control,缩写“PCC” ...