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

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

最后通过start 方法就可。

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

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

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>

源代码:点击打开链接

Android学习笔记进阶十一图片动画播放(AnimationDrawable)的更多相关文章

  1. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  2. Android学习笔记进阶16之BitmapShader

    <1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public   BitmapShader(Bitmap bitmap,Shader.TileMode ti ...

  3. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...

  4. Android学习笔记进阶18 之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  5. Android学习笔记进阶18之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  6. Android学习笔记(十一)——ListView的使用(下)

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! ListView 这个控件比较复杂, 就是因为它有很多的细节可以优化,下面我们在试试提高它的运行效率:一.提高 ...

  7. Android 学习笔记进阶十二之裁截图片

    package xiaosi.cut; import java.io.File; import android.app.Activity; import android.content.Intent; ...

  8. Android学习笔记进阶20 之得到图片的缩略图

    <1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...

  9. Android学习笔记进阶20之得到图片的缩略图

    <1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...

随机推荐

  1. C语言移位

    先说左移,左移就是把一个数的所有位都向左移动若干位,在C中用<<运算符.例如: int i = 1;i = i << 2;  //把i里的值左移2位 也就是说,1的2进制是00 ...

  2. struts2文件过滤拦截器fileUpload以及各种文件类型

    本文某些内容复制自:http://zhidao.baidu.com/link?url=F0Z-FqbZ83BOj_xXp_B8rgJDzUoeVSWGgXwPNP5fEdLU1nvBK7yO4vnX_ ...

  3. BZOJ3158: 千钧一发

    [传送门:BZOJ3158] 简要题意: 给出n个机器,每个机器有a[i]基础值和b[i]价值 选出一部分机器使得这些机器里面两两至少满足以下两种条件之一: 1.a[i]2+a[j]2!=T2(T为正 ...

  4. legend---四、菜鸟教程css3里面有教你炫酷的按钮怎么做

    legend---四.菜鸟教程css3里面有教你炫酷的按钮怎么做 一.总结 一句话总结:想学,总是有很多资料的 1. 自动居中是 margin:100px 0px;么? 自动居中是margin:100 ...

  5. kafka查询topic属性含义

    第一行,列出了topic的名称,分区数(PartitionCount),副本数(ReplicationFactor)以及其他的配置(Config.s) Leader:1 表示为做为读写的broker的 ...

  6. Hadoop的单节点集群详细启动步骤

    见,如下博客 hadoop-2.2.0.tar.gz的伪分布集群环境搭建(单节点) 很简单,不多赘述.

  7. Bundles软件

    Bundle 称为:软件集 或 打包捆绑软件(软件束) Bundle就是一组包含了文件集,软件包或许可程序产品的软件,它们组合在一起为了实现一个特定的功能     快速来列出系统bundle软件 sm ...

  8. vue中eventbus的使用

    eventbus的方法很是简单,我们需要做三步事情: 第一步,我们需要创造一个容器去充当我们的eventbus 第二步,我们需要去抛出,或者说提交我们的事件 第三步,我们去监听我们的那个事件(也许这才 ...

  9. java实现折半查找

    package althorgrim;/** * 1.必须采用顺序存储结果 * 2.关键字必须有序 * @author hanrk-2734 * */public class TestBinarySe ...

  10. 【APP测试】APP弱网环境测试

    方法一:利用抓包工具 1.利用fiddler通过代理连接上手机之后,进入Fiddler->Rules->Customize Rules,点击弹出的CustomRules.js文件,找到m_ ...