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 ...
随机推荐
- Redis整合Spring结合使用缓存实例(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
- HDU 4777 Rabbit Kingdom(树状数组)
HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...
- WEB 3D SVG CAD 向量 几个实施
一.他们所有的发展.从地上爬起来 VML+SVG发展矢量地图.你并不需要导入第三方的图片作为背景,直接在地图编辑器可以在底图内容编辑,由于岩石.巷道.煤层.画水.础地图样子再在其上面画出智慧线等设备, ...
- Nutch 二次开发parse纸
大约nutch基础知识可以参考lemo柱 nutch支持二次开发,为了满足搜索的准确性的问题,内容提取出来作为索引的内容,相应的是parse_text的数据.我使用的事nutch1.4 版本号,在cy ...
- WTL安装
1.在AppWiz文件夹下有多个JScript文件,依据自己的编辑器选择对应的文件执行. 假设双击无法执行的话,则执行wscript //e:jscript (文件路径) 如wscript //e:j ...
- 开源Math.NET基础数学类库使用(12)C#随机数扩展方法
原文:[原创]开源Math.NET基础数学类库使用(12)C#随机数扩展方法 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...
- DisplayContent、StackBox、TaskStack笔记
文章仅零散记录自己的一点理解,仅供自己參考. 每一个显示设备,都有一个Display对象,DisplayManagerService专门管理这些Display. 1.DisplayContent() ...
- C++该函数隐藏
只有基类成员函数的定义已声明virtualkeyword,当在派生类中的时间,以支付功能实现,virtualkeyword可以从时间被添加以增加.它不影响多状态. easy混淆视听,掩盖: ,规则例如 ...
- JAVA学习(二):JDK介绍及其结构、用途
JDK介绍及其结构.用途 1.JDK的介绍 JDK是Java Development Kit 的缩写.是Sun Microsystems针对Java开发员的产品.它由一个处于操作系统层之上的执行环境还 ...
- 采取Volley,实现瀑布流
今天停止php,在研究Volley框架的源代码,实现了瀑布流的效果. 为了实现最终的级联效应,一些需要掌握的知识: (1)自己定义布局,由于我们要监听滑究竟部的事件就要实现自己定义的ScrollVie ...