上一页介绍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帧动画)的更多相关文章

  1. 转Android 用Animation-list实现逐帧动画

    Android 用Animation-list实现逐帧动画     第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/arti ...

  2. Android动画系列之帧动画和补间动画

    原文首发于微信公众号:jzman-blog,欢迎关注交流! Android 提供三种动画:帧动画.补间动画和属性动画,本篇文章介绍帧动画以及补间动画的使用,属性动画的使用将在后面的文章中分享,那就来复 ...

  3. Lottie在手,动画我有:ios/Android/Web三端复杂帧动画解决方案

      为什么需要Lottie 在相对复杂的移动端应用中,我们可能会需要使用到复杂的帧动画.例如: 刚进入APP时候可能会看到的入场小动画,带来愉悦的视觉享受 许多Icon的互动变化比较复杂多变的时候,研 ...

  4. Android为TV端助力 帧动画

    首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...

  5. Android动画之逐帧动画(FrameAnimation)详解

    今天我们就来学习逐帧动画,废话少说直接上效果图如下: 帧动画的实现方式有两种: 一.在res/drawable文件夹下新建animation-list的XML实现帧动画 1.首先在res/drawab ...

  6. Android 用Animation-list实现逐帧动画 (转载)

    转自:http://blog.csdn.net/aminfo/article/details/7847761 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog ...

  7. android 动画基础绘——帧动画(三)

    前言 这篇介绍帧动画. 什么是帧动画? 帧动画,非常好理解.就是轮播,比如我们看电视,其实就是一张一张播放过去的. 正文 <?xml version="1.0" encodi ...

  8. Android 用Animation-list实现逐帧动画

    第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/article/details/7847761 图片素材: 文件名称: ic ...

  9. animation steps属性实现帧动画

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

随机推荐

  1. HDU 4435 charge-station (并查集)

    先说下题目的意思: 在一个二维坐标系中有N个点,某人要来个走遍所有点的旅行,但是他的车每次加油后只能走M个单位距离:所以要在这个N点中选一些建立加油站:问题来了:i^th  点 建加油站的花费是  2 ...

  2. 两个堆叠fragment,上层fragment响应于降低fragment的button点击事件补救措施

    加入onViewCreated的Touch事件监听, 以解决叠在一起的fragment上层响应下层的button点击事件解决方法 @Override public void onViewCreated ...

  3. 理解和运用javascript中的call及apply

    call是为了改变函数上下文context而存在的,换言之,就是改变函数内部this的指向.因为javascript存在[定义时上下文],[运行时上下文]及[上下文]是可以改变的.例如:var fun ...

  4. 关于scope_identity()与 @@IDENTITY

    原文:关于scope_identity()与 @@IDENTITY 参考:https://msdn.microsoft.com/zh-cn/library/ms190315.aspx scope_id ...

  5. MVC5 Entity Framework学习参加排序、筛选和排序功能

    上一篇文章实现Student 基本的实体CRUD操作.本文将展示如何Students Index页添加排序.筛选和分页功能. 以下是排序完成时.经过筛选和分页功能截图,您可以在列标题点击排序. 1.为 ...

  6. chrome(转)

    阅读目录 Chrome的隐身模式 Chrome下各种组合键 Chrome的about指令 chrome://accessibility     查看浏览器当前访问的标签 chrome://appcac ...

  7. C++ - 内置类型的最大值宏定义

    内置类型的最大值宏定义 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24311895 C++中, 常常会使用, 某些类型的最大值 ...

  8. Hibernate HQL详细说明

    1.  Hibernate HQL详细说明 1.1.  hql一个简短的引论 Hibernate它配备了一种非常强大的查询语言.这种语言看起来非常像SQL.但是不要 要对相位的语法结构似,HQL是很有 ...

  9. 行政歌节 &#183; 萧谱1

    4之前听 陈越 的<绿野仙踪> 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  10. Python调用微博API

    上头叫通过微博ID获取用户公布过的历史微博内容,于是研究了下新浪微博提供的API 1 首先在微博开放中心下"创建应用"创建一个应用,应用信息那些随便填,填写完成后,不须要提交审核, ...