Android学习笔记进阶十一图片动画播放(AnimationDrawable)

大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个

AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间,本例子中间隔时间为5S,

最后通过start 方法就可。

以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。

  1. package xiaosi.bu;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.AnimationDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10.   
  11. public class TupianActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     private Button start = null;  
  14.     private Button stop = null;  
  15.     private ImageView image = null;  
  16.     private AnimationDrawable animationDrawable = null;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.           
  22.         start = (Button)findViewById(R.id.start);  
  23.         start.setOnClickListener(new StartListener());  
  24.         stop = (Button)findViewById(R.id.stop);  
  25.         stop.setOnClickListener(new StopListener());  
  26.           
  27.         image = (ImageView)findViewById(R.id.imageview);  
  28.           
  29.         animationDrawable = new AnimationDrawable();  
  30.         for(int i =0;i<8;i++){  
  31.             //第一个 就是我们的资源名称(图片名)    
  32.             //第二个 就是我们存放图片的文件夹drawable    
  33.             //第三个 包名也可以用Context的getPackageName返回应用程序的包名    
  34.             int id = getResources().getIdentifier( "a"+i, "drawable", "xiaosi.bu");  
  35.             System.out.println("ID:" + id);  
  36.             animationDrawable.addFrame(getResources().getDrawable(id), 2000);  
  37.         }  
  38.       //设置手否重复播放,false为重复  
  39.         animationDrawable.setOneShot(false);  
  40.         image.setImageDrawable(animationDrawable);  
  41.   
  42.     }  
  43.     private class StartListener implements OnClickListener{  
  44.   
  45.         public void onClick(View v)  
  46.         {  
  47.             animationDrawable.start();  
  48.         }  
  49.     }  
  50.       
  51.     private class StopListener implements OnClickListener{  
  52.   
  53.         public void onClick(View v)  
  54.         {  
  55.             animationDrawable.stop();    
  56.         }  
  57.     }  
  58. }  


main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:orientation="vertical"   
  4.         android:layout_width="fill_parent"  
  5.        android:layout_height="fill_parent">  
  6.        <LinearLayout  
  7.         android:orientation="horizontal"   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">  
  10.        <Button android:id="@+id/start"  
  11.            android:text="Start"   
  12.            android:layout_width="wrap_content"  
  13.            android:layout_height="wrap_content"/>  
  14.        <Button android:id="@+id/stop"  
  15.            android:text="End"  
  16.            android:layout_width="wrap_content"  
  17.            android:layout_height="wrap_content"/>  
  18.        </LinearLayout>  
  19.        <ImageView android:id="@+id/imageview"   
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="fill_parent"   
  22.            android:scaleType="fitXY"   
  23.            android:background="#ffffff" />  
  24.  </LinearLayout>  


 

 

源代码:点击打开链接

posted @   brave-sailor  阅读(387)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2016-04-17 窥探 Swift 之 函数与闭包的应用实例
2016-04-17 swift 深入理解Swift的闭包
2016-04-17 Swift开发语法
2016-04-17 Swift学习笔记 - 函数与闭包
2016-04-17 那些年,学swift踩过的坑
2014-04-17 android之TabWidget选项卡
点击右上角即可分享
微信分享提示