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;// 开始动画按钮
	private Button stopBtn;// 停止动画按钮
	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();
		// 按钮事件
		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;// 开始动画按钮
	private Button stopBtn;// 停止动画按钮
	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逐帧动画),布布扣,bubuko.com

时间: 2024-08-14 06:53:25

Android Animation 动画Demo(Frame逐帧动画)的相关文章

Android动画效果之Frame Animation(逐帧动画)(二)(

前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame Animation(逐帧动画). Frame Animation(逐帧动画): 逐帧动画(Frame-by-frame Animations)从字面上理解就是一帧挨着一帧的播放图片,就像放电影一样.和补间动画一样可以通过xml实现也可以通过java代码实现.接下来借助目前项目中的一个开奖的动画来总结

Android动画效果之Frame Animation(逐帧动画)

前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame Animation(逐帧动画). 其他几种动画效果: Android动画效果之Tween Animation(补间动画) Android动画效果之Frame Animation(逐帧动画) Android动画效果之初识Property Animation(属性动画) Android动画效果之Prop

Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)

就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. android提供两种方法为AnimationDrawable添加帧:XML定义和JAVA代码创建. XML 因为动画帧的资源需要是一个Drawable对象,所以需要把它放到Drawable目录下.在<animation-list>使用<item>来添加一帧 anima.xml <?xml

Android 学习之逐帧动画(Frame)

帧动画就是将一些列图片.依次播放. 利用肉眼的"视觉暂留"的原理,给用户的感觉是动画的错觉,逐帧动画的原理和早期的电影原理是一样的. a:须要定义逐帧动画,能够通过代码定义.也能够通过XML文件定义.一般XML文件定义比較直观 <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/

css3 animation实现逐帧动画

css3里面的animation属性非常强大,但是自己用的比较少,最近有次面试就刚好被问到了,趁现在有时间就对animation做一个小总结.同时实现一个逐帧动画的demo作为练习 animation属性一览 因为animation属性比较多,然后在w3c上看有点蛋疼,干脆也做了一份导图,以后想查看,就一目了然了 使用animation实现逐帧动画 熟悉了animation的属性之后,得找个简单的小项目实现下,逐帧动画好有意思,先跑一个满足下自己思路很简单,就是给元素一个雪碧图的背景,然后添加的

android 逐帧动画的播放与播放结束操作对象

逐帧动画的xml代码: 1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 3 android:oneshot="true" > 4 5 <item 6 android:drawable="@drawabl

Android中的动画具体解释系列【1】——逐帧动画

逐帧动画事实上非常easy,以下我们来看一个样例: <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawa

Android -- 逐帧动画

在处理耗时工作的时候,大多数会弹出一个加载的框,里面有一个连续旋转的图片,很多时候都是用一张图片,使用rotate来设定旋转,不过看起来不太美观,没有形象感,在3.0之前Android有两种动画效果分别是补间动画和帧动画,用一张图片实现的是使用补间动画,定义给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变. 动画布局                                                                               

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

Android 用Animation-list实现逐帧动画 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/article/details/7847761 图片素材: 文件名称: icon1.png icon2.png icon3.png icon4.png icon5.png icon6.png 第二步:上动画Animation-list帧布局文件,有2个,一个是按顺序显示动画,一个是倒序显示动画,文件存放在res/d