码迷,mamicode.com
首页 > 移动开发 > 详细

android动画-Frame Animation

时间:2015-01-15 14:22:51      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:frame   animation   android   

转载请注明出处: http://blog.csdn.net/forwardyzk/article/details/42739281

Drawable Animation(Frame Animation):帧动画,连续播放和播放gif图片的效果是一样的。

可以使用xml构建Drawable Animation,也可以使用代码编写Drawable Animation动画

下面先介绍使用xml构建Drawable Animation动画

在res目录下新建anim目录

创建xml文件

例如:

drwable_fr.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/bird1"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird2"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird3"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird4"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird5"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird6"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird7"
        android:duration="300"/>
    <item
        android:drawable="@drawable/bird8"
        android:duration="300"/>

</animation-list>



必须以animation-list为根节点,item为子节点,

android:oneshot="false":连续播放,true:表示只播放一次

android:drawable="@drawable/bird1" 制定播放的帧图片

android:duration="300" 帧播放的时间


在代码中指定View的播放动画

image = (ImageView) findViewById(R.id.image);
		image.setBackgroundResource(R.anim.drwable_fr);
		drwableAnimation = (AnimationDrawable) image.getBackground();
		drwableAnimation.start();
		drwableAnimation.stop();

先指定帧动画资源为View的背景,获取背景,转换为AnimationDrawable,

start()开始播放,stop为停止播放


下面介绍使用代码构建Drwable Animation

anim = new AnimationDrawable();
		for (int i = 1; i <= 3; i++) {
			// 根据资源名称和目录获取R.java中对应的资源ID
			int id = getResources().getIdentifier("jump" + i, "drawable",
					getPackageName());
			// 根据资源ID获取到Drawable对象
			Drawable drawable = getResources().getDrawable(id);
			// 将此帧添加到AnimationDrawable中
			anim.addFrame(drawable, 300);
		}
		anim.setOneShot(false); // 设置为loop

int id = getResources().getIdentifier("jump" + i, "drawable",getPackageName());

第一个参数为:图片的名字,第二个参数:图片所在的目录,第三个参数:应用的包名

技术分享


形成Drawable对象:Drawable drawable = getResources().getDrawable(id);

添加到动画中:anim.addFrame(drawable, 300);

anim.setOneShot(false);设置是否循环播放,false:是,true,只播放一次


注意:
不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。

源代码下载: http://download.csdn.net/detail/forwardyzk/8363995

效果图:

技术分享





android动画-Frame Animation

标签:frame   animation   android   

原文地址:http://blog.csdn.net/forwardyzk/article/details/42739281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!