Android中的动画实现详解(2)--逐帧动画

       下面就开始各动画方式的介绍,首先介绍的是逐帧动画。

       逐帧动画是Android中最简单的动画,主要你拥有动画的所有的静态图片,那么你就可以实现该动画。虽然该动画实现简单,但是却反映了动画的真正的本质,只不过在视图动画中,Android使用算法为你生成了给定时间段中的静态图片而已。

一、AnimationDrawable类的简单介绍

       为了实现将图片逐帧播放的功能,Android提供了类AnimationDrawable,该类一般是作为某一个View的background来使用。虽然在AnimationDrawable类中提供addFrame函数来为该动画添加一帧图片,但是建议使用XML文件来为该动画配置图片,这样子更简单而且易于管理。

       定义逐帧动画的XML文件非常的简单,只要在<animation-list.../>元素中使用<item.../>子元素定义动画的全部帧,并且制定各帧的持续时间即可(为了保证动画的流畅度,建议保证1秒24帧的速度播放,也就是说,动画持续时间最好小于1000/24)。所以得到逐帧动画的具体的定义格式如下:

<?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>
       其中android:onshot控制该动画是否为循环播放,如果为true,则不会循环播放;否则就会循环播放。

       那么在Android中是如何使用AnimationDrawable类来实现逐帧动画的呢?

       其实AnimationDrawable就是一个Runnable,它其实就是启动一个线程来逐个显示动画中的静态图片,下面通过类中的几个函数就可以很容易明白这一点了。

run函数的定义:

public void run() {
    nextFrame(false);
}

start函数的定义:

public void start() {
    if (!isRunning()) {
        run();
    }
}

stop函数的定义:

public void stop() {
    if (isRunning()) {
        unscheduleSelf(this);
    }
}

二、逐帧动画的实现步骤:

       通过上面的分析,我们在这里总结一下实现逐帧动画的步骤。

       1)在XML文件中定义动画所需要的静态图片;

       2)将该动画设置成为一个View的background;

       3)在代码中通过getBackground函数获取该View的background;

       4)调用AnimationDrawable中的start启动动画。

三、逐帧动画的实例

       下面通过一个实例来体验一下实现逐帧动画的过程。

       1)在res中anim文件夹中新建一个定义动画的文件xml文件music_loading.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/music_load1" android:duration="500" />
	<item android:drawable="@drawable/music_load2" android:duration="500" />
	<item android:drawable="@drawable/music_load3" android:duration="500" />
	<item android:drawable="@drawable/music_load4" android:duration="500" />
	<item android:drawable="@drawable/music_load5" android:duration="500" />
	<item android:drawable="@drawable/music_load6" android:duration="500" />
	<item android:drawable="@drawable/music_load7" android:duration="500" />
	<item android:drawable="@drawable/music_load8" android:duration="500" />
	<item android:drawable="@drawable/music_load9" android:duration="500" />
	<item android:drawable="@drawable/music_load10" android:duration="500" />
	<item android:drawable="@drawable/music_load11" android:duration="500" />
	<item android:drawable="@drawable/music_load12" android:duration="500" />
	<item android:drawable="@drawable/music_load13" android:duration="500" />
	<item android:drawable="@drawable/music_load14" android:duration="500" />
</animation-list>

       2)在定义的layout文件中将该动画设置为一个view的background,该实例中使用ImageView

<?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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
    <ImageView
        android:id="@+id/musicLoader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/music_loading"/>

    <Button 
        android:id="@+id/startFrame"
        android:text="Start..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <Button 
        android:id="@+id/stopFrame"
        android:text="Stop..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

       3)在Java文件中获取animation,同时启动

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;

public class FrameAnimation1Activity extends Activity {
    /** Called when the activity is first created. */
	private ImageView musicLoader;
	private Button startBtn;
	private Button stopBtn;
	private AnimationDrawable loadingAnimation;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        musicLoader = (ImageView)findViewById(R.id.musicLoader);
        //第三步:通过getBackground获取AnimationDrawable对象
        loadingAnimation = (AnimationDrawable)musicLoader.getBackground();
        
        startBtn = (Button)findViewById(R.id.startFrame);
        //第四步:通过start方法启动动画
        startBtn.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		loadingAnimation.start();
        	}
        });
        
        stopBtn = (Button)findViewById(R.id.stopFrame);
        //第四步:通过stop方法停止动画
        stopBtn.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		loadingAnimation.stop();
        	}
        });
    }
}


 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值