【Android】Frame Animation

        Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画。Frame动画可以被定义在XML文件中,也可以完全编码实现。

        如果被定义在XML文件中,我们可以放置在/res下的anim或drawable目录中(/res/[anim | drawable]/filename.xml),文件名可以作为资源ID在代码中引用;如果由完全由编码实现,我们需要使用到AnimationDrawable对象。

       如果是将动画定义在XML文件中的话,语法如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource_name"
        android:duration="integer" />
</animation-list>

需要注意的是:

<animation-list>元素是必须的,并且必须要作为根元素,可以包含一个或多个<item>元素;android:onshot如果定义为true的话,此动画只会执行一次,如果为false则一直循环。

<item>元素代表一帧动画,android:drawable指定此帧动画所对应的图片资源,android:druation代表此帧持续的时间,整数,单位为毫秒。

下面来个具体的实例,我们将若干张图片放在drawable目录中,然后建一个test_animation.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:visible="true" />
<item
    android:drawable="@drawable/dropdown_anim_00"
    android:duration="100"/>
<item
    android:drawable="@drawable/dropdown_anim_01"
    android:duration="100"/>
<item
    android:drawable="@drawable/dropdown_anim_02"
    android:duration="100"/>
<item
    android:drawable="@drawable/dropdown_anim_03"
    android:duration="100"/>
<item
    android:drawable="@drawable/dropdown_anim_04"
    android:duration="100"/>
<item
    android:drawable="@drawable/dropdown_anim_05"
    android:duration="100"/>
<item
    android:drawable="@drawable/dropdown_anim_06"
    android:duration="100"/>
<animation-list>


</animation-list>

我们可以将test_animation.xml文件放置于drawable或anim目录,官方文档上是放到了drawable中了,大家可以根据喜好来放置,放在这两个目录都是可以运行的。

再来看一下具体实现

@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		// 用XML实现Frame Animation
		animationImg1.setBackgroundResource(R.drawable.test_animation);
		AnimationDrawable animationDrawable = (AnimationDrawable) animationImg1
				.getBackground();
		animationDrawable.start();
	}

只用Java代码实现Frame Animation

public void testAnimationImg(View view) {
		// 用Java代码实现Frame Animation
		AnimationDrawable animationDrawable = new AnimationDrawable();
		for (int i = 0; i <= 9; i++) {
			// 获取资源的ID--(参数1:资源名称(文件名),参数2:所在名录)
			int id = getResources().getIdentifier("dropdown_anim_0" + i,
					"drawable", getPackageName());
			Drawable drawable = getResources().getDrawable(id);
			animationDrawable.addFrame(drawable, 200);// 持续200ms
		}

		// 只显示一次
		animationDrawable.setOneShot(true);
		// 将动画添加到ImageView中
		animationImg2.setBackgroundDrawable(animationDrawable);
		// 开始动画
		animationDrawable.start();
	}


完整代码

package com.example.learn.activity;

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.Window;
import android.widget.ImageView;

import com.example.learn.R;

/**
 * 
 * Description: Frame Animation Instance
 * 
 * @author danDingCongRong
 * @Version 1.0.0
 * @Created at 2014-5-26 22:09:13
 * @Modified by [作者] on [修改日期]
 */

public class TestAnimationActivity extends Activity {

	private ImageView animationImg1;
	private ImageView animationImg2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_test_animation);

		initView();
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		// 用XML实现Frame Animation

		// 将动画资源作为ImageView的背景
		animationImg1.setBackgroundResource(R.drawable.test_animation);
		AnimationDrawable animationDrawable = (AnimationDrawable) animationImg1
				.getBackground();
		animationDrawable.start();
	}

	private void initView() {
		animationImg1 = (ImageView) findViewById(R.id.animationImg1);
		animationImg2 = (ImageView) findViewById(R.id.animationImg2);
	}

	public void testAnimationImg(View view) {
		// 用Java代码实现Frame Animation
		AnimationDrawable animationDrawable = new AnimationDrawable();
		for (int i = 0; i <= 6; i++) {
			// 获取资源的ID--(参数1:资源名称(文件名),参数2:所在名录)
			int id = getResources().getIdentifier("dropdown_anim_0" + i,
					"drawable", getPackageName());
			Drawable drawable = getResources().getDrawable(id);
			animationDrawable.addFrame(drawable, 200);// 持续200ms
		}

		// 只显示一次
		animationDrawable.setOneShot(true);
		// 将动画添加到ImageView中
		animationImg2.setBackgroundDrawable(animationDrawable);
		// 开始动画
		animationDrawable.start();
	}
}


更多参考:点击打开链接



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值