Android(java)学习笔记198: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)学习笔记198:Android下的逐帧动画(Drawable Animation)的更多相关文章
- Android(java)学习笔记141: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 ...
随机推荐
- 重读gets()与is函数的用法
这是从百度百科上查找的资料: gets(): 从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中.换行符不作为读取串的内容,读取的换行符 ...
- android string[] arraylist<string>互转
string[] to arraylist<string>:eg:string[] str; arraylist<string> arr; for(int i=0;i<s ...
- 微服务、SOA 和 API:是敌是友?
为一个正在不断发展的企业对比关键的集成与应用程序架构概念 对比微服务架构和面向服务的架构(SOA)是一个敏感的话题,常常引起激烈的争论.本文将介绍这些争论的起源,并分析如何以最佳方式解决它们.然后进一 ...
- HDU 4884 TIANKENG’s rice shop (模拟)
TIANKENG's rice shop 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/J Description TIANKE ...
- Contest 7.23(不知道算什么)
Problem A URAL 1181 Cutting a Painted Polygon 题目大意就是说有一个N边形,让你做N-3条边,让他们的每个三角形的三个顶点颜色都不相同. 这里有一个引理 ...
- Arduino Due, Maple and Teensy3.0 的 W5200性能测试
开源平台中以太网连接方案里W5100是众所周知的,W5200正在此领域越来越受欢迎.这个测试结果是在Arduino Due(Atmel CortexM3-84Mhz), Maple(ST Cortex ...
- 在Tomcat上运行ADF Essentials应用
Tomcat 7.0.32,Oracle ADF Essentials 12.1.2 Oracle ADF Essentials是Oracle ADF框架的免费版本.Oracle ADF essent ...
- android studio 2.0 Gradle HttpProxy 设置
Android Studio 一直Failed to import Gradle project: Connection timed out: connect Android Studio 2.0 里 ...
- 剑指OFFER之链表中倒数第k个节点(九度OJ1517)
题目描述: 输入一个链表,输出该链表中倒数第k个结点.(hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入的第一行为两个整数n和k(0<= ...
- Velocity中#set指令
引用可以让模板设计者生成动态内容,而指令允许设计者真正的负责页面的展现和内容. 指令是以#开头,后面紧跟一个关键字,比如if,else,foreach等.而这个关键字,是可以被放在{}中间的.这是规范 ...