Android通过播放多张图片形成一个动画 分类: Android 2015-04-24 14:05 16人阅读 评论(0) 收藏

Andriod里可以逐帧的播放图片,然后产生一种动态的效果.

1.准备好几张连续的图片,在源程序res文件夹下建立anim文件夹,然后新建一个XML:

<?xml version="1.0" encoding="utf-8"?>   
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    
android:oneshot="true">    
<item android:drawable="@drawable/row01" android:duration="200" />    
<item android:drawable="@drawable/row02" android:duration="200" />    
<item android:drawable="@drawable/row03" android:duration="200" />   
<item android:drawable="@drawable/row04" android:duration="200" /> 
<item android:drawable="@drawable/row05" android:duration="200" /> 
<item android:drawable="@drawable/row06" android:duration="200" />    
<item android:drawable="@drawable/row07" android:duration="200" />   
<item android:drawable="@drawable/row08" android:duration="200" />   
</animation-list>  

2.在窗体里面放置一个ImageView控件,并在代码中编写:
iv_run_row =(ImageView)findViewById(R.id.run_row);//放置的ImageView控件
//设置动画背景 
iv_run_row.setBackgroundResource(R.anim.running_row);//其中R.anim.animation_list就是上一步准备的动画描述文件的资源名 
//获得动画对象 
run_row_animaition = (AnimationDrawable)iv_run_row.getBackground();

3.编写方法,调用该方法即可实现动画播放。

private void runRow() {

//是否仅仅启动一次?
run_row_animaition.setOneShot(false);  
if(run_row_animaition.isRunning())//是否正在运行? 
{   
run_row_animaition.stop();//停止 
}   
run_row_animaition.start();//启动
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/xieping/p/4666313.html