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)的更多相关文章

  1. Android(java)学习笔记198:Android下的逐帧动画(Drawable Animation)

    1.帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一帧 ...

  2. Android开发学习笔记-关于Android的消息推送以及前后台切换

    下面是最简单的Android的消息推送的实现方法 package com.example.shownotic; import java.util.Random; import android.supp ...

  3. 程序设计基础·Java学习笔记·面向对象(下)

    Java程序设计基础之面向对象(下) (补充了上的一些遗漏的知识,同时加入了自己的笔记的ヾ(•ω•`)o) (至于为什么分P,啊大概是为了自己查笔记方便(?)应该是("` 3′") ...

  4. java学习笔记1——window7下JDK环境变量配置图解

    1. 首先下载Java安装工具包   http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.ht ...

  5. Java学习笔记之Linux下的Java安装和配置

    0x00 概述 由于使用 yum 或者 apt-get 命令 安装 openjdk 可能存在类库不全,从而导致用户在安装后运行相关工具时可能报错的问题,所以此处我们推荐采用手动解压安装的方式来安装 J ...

  6. Java学习笔记(面向对象下)

    面向对象(下) 类的继承 类的继承是指在一个现有类的基础上去构建一个新的类,构建出来的新类称为子类,现有类称为父类,子类会自动拥有父类所有可继承的属性和方法.(用extends关键字)   //定义A ...

  7. Android 数字签名学习笔记

    Android 数字签名学习笔记 在Android系统中,所有安装到系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程序之间建立信任关系,如果一个permission的pro ...

  8. Android:日常学习笔记(6)——探究活动(4)

    Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...

  9. Android Studio 学习笔记(一)环境搭建、文件目录等相关说明

    Android Studio 学习笔记(一)环境搭建.文件目录等相关说明 引入 对APP开发而言,Android和iOS是两大主流开发平台,其中区别在于 Android用java语言,用Android ...

随机推荐

  1. Angular JS ng-repeat 报错 Error: [ngRepeat:dupes]

    ng-repeat常用情况: <div class="form-group" ng-repeat="item in items"></div& ...

  2. java——注解处理器

    Java提供了两种方式来处理注解:第一种是利用运行时反射机制:另一种是使用Java提供的API来处理编译期的注解. 运行时通过反射:仅当定义的注解的@Retention为RUNTIME时,才能够通过运 ...

  3. Index Skip Scan in Oracle in 11g

    http://viralpatel.net/blogs/oracle-index-skip-scan/ in 11g the same sql use index skip scan but in 1 ...

  4. Java程序员进阶架构师推荐阅读书籍

    [IT168 技术]作为Java程序员来说,最痛苦的事情莫过于可以选择的范围太广,可以读的书太多,往往容易无所适从.我想就我自己读过的技术书籍中挑选出来一些,按照学习的先后顺序,推荐给大家,特别是那些 ...

  5. AndroidAPI

    1词典 百度翻译 http://api.fanyi.baidu.com/api/trans/product/index 百度翻译支持多种语言互相翻译,包含PHP, JS, Python, C, Jav ...

  6. Tomcat WEB搭建+Nginx负载均衡动静分离+DNS解析的实验

    实验拓扑图: 实验环境: 在VMware workstation搭建虚拟环境,利用网络适配器的Nat和桥接模式模拟内网和外网环境. 实验过程中需要安装的工具包包括:vim unzip lrzsz ls ...

  7. @enable跟@import注解

    参考文章: 讲@import的相关内容:https://blog.csdn.net/u012437781/article/details/78626134 讲为什么registrar没有注入:http ...

  8. java多线程之原子变量

    看链接博客:http://blog.csdn.net/u011116672/article/details/51068828

  9. spring的IOC和AOP详细讲解

    1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是有这个IOC容器进行产生,同时, ...

  10. httpd 的坑

    Httpd服务器的坑 在/etc/httpd/conf/httpd.conf中的配置信息, 有时注释到的内容仍然会生效 配置Auth时, 允许htpasswd规定的文件中的所有的用户, Require ...