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 ...
随机推荐
- zoj3829 Known Notation --- 2014 ACM-ICPC Asia Mudanjiang Regional Contest
根据规则,可以发现,一*之前必须有至少2数字.一(11*)这反过来可以被视为一数字. 因此,总位数必须大于*号码,或者你会加入数字. 添加数字后,,为了确保该解决方案将能够获得通过交流.那么肯定是*放 ...
- CF(427D-Match & Catch)后缀数组应用
题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等于1.出现的定义为:能够重叠的出现. 解法:后缀数组的应用.从小枚举长度.假设一个长度len合法的话:则一定存在这个样的s ...
- Hadoop入门进阶步步高(五)-搭建Hadoop集群
五.搭建Hadoop集群 上面的步骤,确认了单机能够运行Hadoop的伪分布运行,真正的分布式运行无非也就是多几台slave机器而已,配置方面的有一点点差别,配置起来就很easy了. 1.准备三台se ...
- POJ 1745 Divisibility (线性dp)
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10598 Accepted: 3787 Des ...
- Android ListView分页载入(服务端+android端)Demo
Android ListView分页载入功能 在实际开发中经经常使用到,是每一个开发人员必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了測试. 服务端使用MyEcli ...
- 【原创】构建高性能ASP.NET站点之三 细节决定成败
原文:[原创]构建高性能ASP.NET站点之三 细节决定成败 构建高性能ASP.NET站点之三 细节决定成败 前言:曾经就因为一个小小的疏忽,从而导致了服务器崩溃了,后来才发现:原来就是因为一个循环而 ...
- Windows 8本地化多语言支持
原文:Windows 8本地化多语言支持 在Win8平台处理本地化多语言的支持相对比较容易的,但比WP8稍微复杂一点,并不像WP8平台那样大部分工作都有VS IDE处理,Win8平台的操作基本需要开发 ...
- Snail—ORACLE基础之事务学习(五)
---------------事务---当运行到commit时 事务才算是完毕,不然 会运行rollback操作. declare v_money acount.money%type:=1223; e ...
- ios至于理解锚
锚点ios出现在少数地方,多数用在动画. 今天看了一部电影,以上所有关于锚,两年前锚这个概念看cocos2d当被接触的基本概念,当时我没怎么看,今天看了,刚刚好学习. 阅读blog,它是关于锚,像: ...
- BC 2015在百度之星程序设计大赛 - 预赛(1)(矩形区域-旋转卡)
矩形区域 Accepts: 717 Submissions: 1619 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...