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 ...
随机推荐
- pip 安装库的时候使用豆瓣镜像 提升效率
由于众所周知的原因,国内网络环境始终处于水深火热之中,python库的安装也不例外. 比如在安装 PyQt5-tools 的时候,网速奇慢无比. 好在国内有不少镜像服务源,以豆瓣为例,网速突飞猛进 使 ...
- 1.2 rust cargo
cargo是rust的编译与打包工具,可将rust打包成为一个可执行性文件.生成的可执行性文件不能跨系统的大版本,比如在linux7上打包,那么程序无法在linux6上执行. # cargo new ...
- 二分搜索 - Binary Search
二分搜索是一种在有序数组中寻找目标值的经典方法,也就是说使用前提是『有序数组』.非常简单的题中『有序』特征非常明显,但更多时候可能需要我们自己去构造『有序数组』.下面我们从最基本的二分搜索开始逐步深入 ...
- 在Mac上配置iTerm2+Oh-My-Zsh&配置主题
本教程基本完全按照iTerm2 + Oh My Zsh 打造舒适终端体验配置 但是个人感觉博主的颜色搭配不合理,体现在补全命令的字体不清晰,提示命令与背景颜色太过相近 所以,再此之后使用了Bullet ...
- SpringCloud+Redis
redis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写与硬盘读写的差别,所以常常用作缓存,用于少写多读的场景下 ...
- vue 状态管理vuex(九)
通过props 及 $emit在父子组件通讯,对应频繁更新状态考虑使用vuex store.js export default { // 存储状态值 state: { count: 0 }, // 状 ...
- Python操作列表
1.List Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...
- sql server 2017安装
下载: 1. 2. 3. 安装步骤: https://www.cnblogs.com/ksguai/p/5869558.html 管理工具: Microsoft SQL Server Manageme ...
- Fixed Function Shader
Fixed function shader(固定管线着色器) Shader "Custom/Text01" { //shader名称 ...
- [HZOI 2015]复仇的序幕曲
[题目描述] 你还梦不梦痛不痛,回忆这么重你怎么背得动 ----序言 当年的战火硝烟已经渐渐远去,可仇恨却在阿凯蒂王子的心中越来越深 他的叔父三年前谋权篡位,逼宫杀死了他的父王,用铁血手腕平定了国内所 ...