Android(java)学习笔记141:Android下的逐帧动画(Drawable Animation)
1. 帧动画:
帧动画顾名思义,一帧一帧播放的动画就是帧动画。 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一帧的切换罢了。
2.Android如何实现播放帧动画如下:
(1)首先我在网上下载了一张gif动态图片,如下:

这是一个gif动态图片,其实它是很多静态图片相同区域快速切换,我们怎么样把这个gif动态图片中的静态图片从中抽离出来?
这里我使用了一个小工具gifsplitter(大家可以百度在网上自行下载)
gifsplitter软件截图如下:

(2)然后我们使用上面提到的gifsplitter工具,抽取出上面gif动态图片中的静态图片,运行效果如下:

其中frameslist.gsf是配置文件,大家可以不用理会
(3)上面(1)和(2)只是准备数据源,下面开始是编写代码,实现播放帧动画(一帧一帧播放的动画):
新建一个Android工程,名称为AnimationDemo,如下:

同时在res文件下,新建一个drawable文件夹,把刚才我们抽取的静态图片(frameslist.gsf配置文件不用复制)复制到drawable文件夹中,如下:

这里程序只能识别命名方式为由小写字母、数字和下划线组成的图片,所以我把之前名称改成如上的样子。
(4)布局文件activity_main.xml如下:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"
android:gravity="center_horizontal" > <ImageView
android:layout_width="380dp"
android:layout_height="380dp"
android:id="@+id/iv" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放动画"
android:onClick="play"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放"
android:onClick="stop"
/> </LinearLayout>
(5)上面我们已经引入的图片资源,我们要做的事件就是如何把这些图片关联起来,一帧一帧按照特定的顺序播放形成动画效果,接下来核心关键:
我们在res/drawable文件夹下,定义一个beautiful_img.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/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" />
<item android:drawable="@drawable/img25" android:duration="50" />
<item android:drawable="@drawable/img26" android:duration="50" />
<item android:drawable="@drawable/img27" android:duration="50" />
<item android:drawable="@drawable/img28" android:duration="50" />
<item android:drawable="@drawable/img29" android:duration="50" /> </animation-list>
animation-list是根节点:
•其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画 ,true表示只播放一遍;
•根标签下,通过item标签对动画中的每一个图片进行声明 ;
•android:duration 表示展示所用的该图片的时间长度 (单位:ms);
注意:这里的item的顺序就是我们动画播放的顺序
(6)主代码MainActivity.java:
package com.itheima.frameanim; import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends Activity { //动画引用
AnimationDrawable rocketAnimation; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ImageView rocketImage = (ImageView) findViewById(R.id.iv);
rocketImage.setBackgroundResource(R.drawable.beautiful_img);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
} // public boolean onTouchEvent(MotionEvent event) {
// if (event.getAction() == MotionEvent.ACTION_DOWN) {
// rocketAnimation.start();
// return true;
// }
// return super.onTouchEvent(event);
// } public void play(View view) {
rocketAnimation.start();
}
public void stop(View view) {
rocketAnimation.stop();
} }
ImageView rocketImage = (ImageView) findViewById(R.id.iv); //获取布局文件中的ImageView,用来显示帧动画;
rocketImage.setBackgroundResource(R.drawable.beautiful_img);//设置ImageView的背景资源为beautiful_img.xml
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();//获得帧动画的引用,指向我们定义的beautiful_img.xml
rocketAnimation.start(); //利用AnimationDrawable类的start()方法播放指引的beautiful_img.xml帧动画;
rocketAnimation.stop(); //利用AnimationDrawable类的stop()方法停止播放指引的beautiful_img.xml帧动画;
(7)程序运行在真机上的效果如下:
点击"播放动画",就会播放动画,这里不方便显示手机的动态效果;
点击"停止播放",就会停止播放,再次点击"播放动画",从头(img00)开始播放。

Android(java)学习笔记141:Android下的逐帧动画(Drawable Animation)的更多相关文章
- Android(java)学习笔记198:Android下的逐帧动画(Drawable Animation)
1.帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一帧 ...
- Android开发学习笔记-关于Android的消息推送以及前后台切换
下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...
- 程序设计基础·Java学习笔记·面向对象(下)
Java程序设计基础之面向对象(下) (补充了上的一些遗漏的知识,同时加入了自己的笔记的ヾ(•ω•`)o) (至于为什么分P,啊大概是为了自己查笔记方便(?)应该是("` 3′") ...
- java学习笔记1——window7下JDK环境变量配置图解
1. 首先下载Java安装工具包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...
- Java学习笔记之Linux下的Java安装和配置
0x00 概述 由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 J ...
- Java学习笔记(面向对象下)
面向对象(下) 类的继承 类的继承是指在一个现有类的基础上去构建一个新的类,构建出来的新类称为子类,现有类称为父类,子类会自动拥有父类所有可继承的属性和方法.(用extends关键字) //定义A ...
- Android 数字签名学习笔记
Android 数字签名学习笔记 在Android系统中,所有安装到系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程序之间建立信任关系,如果一个permission的pro ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- Android Studio 学习笔记(一)环境搭建、文件目录等相关说明
Android Studio 学习笔记(一)环境搭建.文件目录等相关说明 引入 对APP开发而言,Android和iOS是两大主流开发平台,其中区别在于 Android用java语言,用Android ...
随机推荐
- Go语言基础之12--Channel
一.不同goroutine之间如何进行通讯? 1.全局变量和锁同步 缺点:多个goroutine要通信时,定义太多的全局变量(每个全局变量功能不一样),不好维护 2.Channel 二.channel ...
- 动态添加表sql
注意:1.tb_wx_userinfo已经存在,直接复制该表结构 DECLARE @manufacturer_id NVARCHAR(10),@sql NVARCHAR(500) SET @manuf ...
- aerospike(1)-centos7安装aerospike
要安装的软件:server和tools 下载地址:https://www.aerospike.com/download/server/4.5.1.5/ server: 1.下载 wget https: ...
- java——时间复杂度、动态数组
O(n)不一定小于O(n^2),要具体来看,而我们说的这种时间复杂度其实是渐进时间复杂度,描述的是n趋近于无穷的情况. 动态数组的时间复杂度: 添加操作:O(n) addLast()的均摊复杂度为O( ...
- 转Linux 下用alias 设置命令别名快速切换常用命令
https://blog.csdn.net/u012830148/article/details/80618616 在linux下开发,经常需要切换目录,如果目录很长则切换起来非常的麻烦,针对一些常用 ...
- Jenkins遇到哪些坑~
1Jenkins关闭和重启实现方式. 1.关闭Jenkins 只需要在访问jenkins服务器的网址url地址后加上exit.例如我jenkins的地址http://localhost:8080/ ...
- Serializable深入理解
1.什么是序列化,解决什么问题 序列化可以对象的状态信息转换成可以持久化或者可以传输形式的过程.一般是转为字节数据.而把字节数组还原成原来同等对象的过程成为反序列化. 在Java中,对象的序列化与反序 ...
- python 矢量化的字符串
- jQuery源代码学习笔记_bind
一般想到JS的兼容性问题的时候,首先会想到addEventListener与attachEvent这一对冤家,那么我们先来看看它们有什么兼容性问题 addEventListener与attachEve ...
- Qt 学习
Qt 学习 C++ 模版 QObject 提供一个十分有用的 api,T findChild(QString, Qt::FindChildOptions),这个函数接收一个模版参数,返回模版参数的类型 ...