您的位置:华清远见教育科技集团 >> Android资料 >> Android帧动画实例详解  
 
Android帧动画实例详解
分享到:

帧动画,是按顺序播放一组图像。帧动画有两个比较重要的属性需要注意,一个是android:oneshot属性,用于设置播放模式,是单次播放还是循环播放;一个是android:duration属性,用于设置每帧的持续时间,单位为毫秒。帧动画的资源文件位于res/anim文件夹下。

为了连续显示一组图像,产生动画效果,Android需要利用XML资源文件和AnimationDrawable来协同工作,下面是Android的一个实现。

XML文件lock_anim.xml的实现如下:

代码:帧动画的处理

……
    < animation-list
            xmlns:android="//schemas.android.com/apk/res/android"
            android:oneshot="false">
            < item android:drawable="@drawable/lock_anim_00" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_02" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_04" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_06" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_08" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_10" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_12" android:duration="400" />
            < item android:drawable="@drawable/lock_anim_14" android:duration="400" />
    < /animation-list>
    …

Android代码的实现如下:

private View mImageView;
    private AnimationDrawable mAnimation;
    mImageView=(ImageView) findViewById(R.id.lock_anim);    //获取视图句柄
    mImageView.setBackgroundResource(R.drawable.lock_anim);    //设置背景资源
    mImageView.setOnClickListener(this);    //设置监听器
    mAnimation=(AnimationDrawable) mImageView.getBackground();    //获取背景
    mAnimation .start()    //播放背景

在默认情况下,AnimationDrawable对背景的播放是循环模式。如果需要改为单次模式,需要设置android:oneshot属性为“true”。

需要注意的是AnimationDrawable的start()方法不能在Activity的onCreate()方法中调用,这是因为此时图像资源尚未完全加载。如果希望能在Activity启动后立即开始动画,可以在Activity的onWindowFocusChanged()方法中执行start()方法调用。

下面是一个具体帧动画例子的实现:

代码:帧动画的实例

package com.miaozl.test;
    import android.app.Activity;
    import android.graphics.drawable.AnimationDrawable;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    public class AnimationActivity extends Activity {
        AnimationDrawable photoAnimation;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.animation);
            ImageView photoImage = (ImageView) findViewById(R.id.anim);
            photoImage.setBackgroundResource(R.anim.animation);
            photoAnimation = (AnimationDrawable) photoImage.getBackground();
        }
        public void onWindowFocusChanged (boolean hasFocus){
            if(hasFocus){ //判断窗口是否已经完全显示
                photoAnimation.start(); //开始动画播放
            }
        }
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                photoAnimation.stop(); //停止动画播放
                return true;
            }
        return super.onTouchEvent(event);
        }
    }

下面是资源文件res/anim-hdpi/animation.xml的实现:

< animation-list xmlns:android="//schemas.android.com/apk/res/android"
    android:oneshot="false">    //设置为循环播放
        < item android:drawable="@drawable/photo1" android:duration="200"/>
        < item android:drawable="@drawable/photo2" android:duration="200" />
        < item android:drawable="@drawable/photo3" android:duration="200" />
        < item android:drawable="@drawable/photo4" android:duration="200" />
        < item android:drawable="@drawable/photo5" android:duration="200" />
        < item android:drawable="@drawable/photo6" android:duration="200" />
    < /animation-list>

帧动画是应用层常用的动画效果,需要说明的是,在Android中,尚不支持gif动画,在播放gif动画时,仅能显示首帧。

 更多相关文章

·Android 控件动画效果的实现
·Android中的四种补间动画
·Android开发中的人脸检测技术
·Android中如何实现图像浏览
·Android图像旋转源码分享