Android Animation 动画Demo(Frame帧动画)
上一页介绍Animation动画第一:Tween吐温动画。
本文介绍了以下Animation也有动画的形式:Frame帧动画。
Frame动画是一系列照片示出的顺序按照一定的处理,和机制,以放电影很阶段似,动画。Frame动画能够被定义在XML文件里。也能够全然编码实现(后面会给出这两种实现方式的源码Demo)。
以下分别介绍:
一、定义在xml中实现:
实现效果图:
源码:
布局文件:main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="以xml文件的形式实现Frame动画"
android:textColor="#000000"
android:textSize="20sp" /> <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" /> <Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放逐帧动画"
android:textColor="#000000" /> <Button
android:id="@+id/stopButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止逐帧动画"
android:textColor="#000000" /> </LinearLayout>
frame.xml:
<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- android:oneshot假设定义为true的话。此动画仅仅会运行一次。假设为false则一直循环。 -->
android:oneshot="false" > <!-- 指定每一帧是使用的图片,及播放时间 -->
<item
android:drawable="@drawable/f1"
android:duration="500">
</item>
<item
android:drawable="@drawable/f2"
android:duration="500">
</item>
<item
android:drawable="@drawable/f3"
android:duration="500">
</item> </animation-list>
FrameDemoActivity:
package com.zhy.com; import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; /**
*
逐帧动画Frame动画实例
*
*/
public class FrameDemoActivity extends Activity {
private Button startBtn;// 開始动画button
private Button stopBtn;// 停止动画button
private ImageView imageView;// 显示图片
private AnimationDrawable anim; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化控件
startBtn = (Button) findViewById(R.id.startButton);
stopBtn = (Button) findViewById(R.id.stopButton);
imageView = (ImageView) findViewById(R.id.image);
// 指定动画的帧的列表
imageView.setBackgroundResource(R.anim.frame);
// AnimationDrawable--与逐帧动画相关的Drawable
anim = (AnimationDrawable) imageView.getBackground();
// button事件
startBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 開始动画
anim.start();
}
});
stopBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
anim.stop();// 停止播放
}
}); }
}
二、直接代码编码的形式实现:
实现效果图:
源码:
布局文件:
activity_main:
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="在代码中直接编码的形式实现Frame动画"
android:textColor="#000000"
android:textSize="20sp" /> <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" /> <Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放逐帧动画"
android:textColor="#000000" /> <Button
android:id="@+id/stopButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止逐帧动画"
android:textColor="#000000" /> </LinearLayout>
MainActivity:
package com.framedemo2; import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener {
private Button startBtn;// 開始动画button
private Button stopBtn;// 停止动画button
private ImageView imageView;// 显示图片
private AnimationDrawable anim; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化控件
startBtn = (Button) findViewById(R.id.startButton);
stopBtn = (Button) findViewById(R.id.stopButton);
imageView = (ImageView) findViewById(R.id.image);
anim = new AnimationDrawable(); startBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this); for (int i = 1; i <= 3; i++) {
// 依据资源名称和文件夹获取R.java中相应的资源ID
int id = getResources().getIdentifier("f" + i, "drawable",
getPackageName());
// 依据资源ID获取到Drawable对象
Drawable drawable = getResources().getDrawable(id);
// 将此帧加入到AnimationDrawable中
anim.addFrame(drawable, 300);
}
anim.setOneShot(false); // 假设设置为false,则仅仅会播放一次。不会循环播放。 imageView.setBackgroundDrawable(anim); // 将动画设置为ImageView背景
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startButton: anim.start();
break;
case R.id.stopButton:
anim.stop();
break; default:
break;
}
} }
以下提供以上两种实现方式的源码,供读者參考使用:
以xml形式实现逐帧动画的源码:
直接以编码的方式实现逐帧动画的源码:
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Android Animation 动画Demo(Frame帧动画)的更多相关文章
- 转Android 用Animation-list实现逐帧动画
Android 用Animation-list实现逐帧动画 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/arti ...
- Android动画系列之帧动画和补间动画
原文首发于微信公众号:jzman-blog,欢迎关注交流! Android 提供三种动画:帧动画.补间动画和属性动画,本篇文章介绍帧动画以及补间动画的使用,属性动画的使用将在后面的文章中分享,那就来复 ...
- Lottie在手,动画我有:ios/Android/Web三端复杂帧动画解决方案
为什么需要Lottie 在相对复杂的移动端应用中,我们可能会需要使用到复杂的帧动画.例如: 刚进入APP时候可能会看到的入场小动画,带来愉悦的视觉享受 许多Icon的互动变化比较复杂多变的时候,研 ...
- Android为TV端助力 帧动画
首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...
- Android动画之逐帧动画(FrameAnimation)详解
今天我们就来学习逐帧动画,废话少说直接上效果图如下: 帧动画的实现方式有两种: 一.在res/drawable文件夹下新建animation-list的XML实现帧动画 1.首先在res/drawab ...
- Android 用Animation-list实现逐帧动画 (转载)
转自:http://blog.csdn.net/aminfo/article/details/7847761 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog ...
- android 动画基础绘——帧动画(三)
前言 这篇介绍帧动画. 什么是帧动画? 帧动画,非常好理解.就是轮播,比如我们看电视,其实就是一张一张播放过去的. 正文 <?xml version="1.0" encodi ...
- Android 用Animation-list实现逐帧动画
第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/article/details/7847761 图片素材: 文件名称: ic ...
- animation steps属性实现帧动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
随机推荐
- C++实现堆排序
堆排序是合并排序和插入排序排序方法共同的长处.它的时间复杂度O(nlgn),这也是一个地方排序算法:在任何时候,外阵中拥有唯一不变的输入数组存储的元素.引进第一家引进什么样的堆堆. 1.建堆:堆数据结 ...
- 好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效button
我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name=& ...
- linux下一个oracle11G DG建立(一个):准备环境
linux下一个oracle11G DG建立(一个):准备环境 周围环境 名称 主库 备库 主机名 bjsrv shsrv 软件版本号 RedHat Enterprise5.5.Oracle 11g ...
- UNITY3D MAC版本号破解
首先,解释一下.是公司做开发建议去购买正版. 之前网上也有非常多人贴出了破解方法,有些也是能够的.可是大多数解说不太具体,在这里贴出相对具体点的教程.本人亲測成功(測试版本Unity4.0.1 mac ...
- 【剑指offer学习】求和为定值的两个数(拓展)
接着上面一篇文章: http://blog.csdn.net/u013476464/article/details/40651451 接下来我们拓展一下题目,如果数组是乱序的,并且规定数组中的元素所有 ...
- 学习swift语言的快速入门教程推荐
随着苹果产品越来越火爆,苹果新推出的swift必定将在很大程度上代替oc语言.学好swift语言,对于IOS工程师来讲,已经是一门必备技能. 有一些比较好的英文版教程,值得学习. 1. Swift T ...
- HDU - 5186 - zhx's submissions (精密塔尔苏斯)
zhx's submissions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 淘宝异构数据源数据交换工具 DataX
淘宝异构数据源数据交换工具 DataX 阅读目录 DataX是什么? DataX用来解决什么? DataX特点? DataX结构模式(框架+插件) DataX在淘宝的运用 DataX是什么? Data ...
- 使用Java快速实现进度条(转)
基于有人问到怎样做进度条,下面给个简单的做法: 主要是使用JProgressBar(Swing内置javax.swing.JProgressBar)和SwingWorker(Swing内置javax. ...
- vs2013提交github代码
vs2013的新特性之一就是可以方便的通过集成的git工具管理git代码.今天简单给大家演示 在github新建仓库 复制仓库地址 克隆仓库到本地(这一步非常重要,只有顺利获取github的code, ...