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 ...
随机推荐
- 恒天云技术分享系列5 – 虚拟化平台性能对比(KVM & VMware)
恒天云技术分享系列:http://www.hengtianyun.com/download-show-id-14.html 概述 本性能测试报告将详细陈述各虚拟化平台基准性能测试的主要结论和详细结果. ...
- redis高级实用特性
1. 安全性 2. 主从复制 3. 事务处理 4. 持久化机制 5. 发布订阅消息 : 可以做一个消息系统 6. 虚拟内存的使用 一 . 安全性 设置客户端连接后进行任何其他指定前需要使用的密码 . ...
- 能够提高开发效率的 Eclipse 实用操作
工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...
- DelphiXE7操作sqlite数据库
准备工作: 1.用SQLiteExpertPers建立一个sqlite数据库. 2.打开delphi xe7. 一.FireDAC法 设置库联接 1.放入FDConnection1控件 2.放入FDC ...
- ocp 1Z0-051 23-70题解析
23. Examine thestructure proposed for the TRANSACTIONS table: name Null Type TRANS_ID NOT NULLNUMBER ...
- #c word转换PDF
需要引用Microsoft.Office.Interop.Word,版本是07之上的. 这个版本会判断文件是否被占用. using Microsoft.Office.Interop.Word; usi ...
- 虚拟攻防系统 HoneyPot
转载原地址 http://www.2cto.com/Article/200410/9.html Honeypot 是一个故意设计为有缺陷的系统,通常是用来对入侵者的行为进行警报或者 诱骗.传统的 Ho ...
- Timus OJ 1997 Those are not the droids you're looking for (二分匹配)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997 这个星球上有两种人,一种进酒吧至少玩a小时,另一种进酒吧最多玩b小时. 下面n行是 ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- SAP-设置显示表格格式
在我们用SAP系统的过程中产看表格的时候,需要设置查看表格的格式,表格的格式主要包含两个方面: 1,表格的样式 在查看表格的时候点击[设置]-[用户参数] 勾选[ALV Grid display]就控 ...