大家平时见到的最多的可能就是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. ArcGIS api for javascript——显示地图属性

    描述 本例展示了如哦读取地图和图层的属性和返回信息给用户.本例中的四个按钮允许用户接收地图属性.每个按钮调用不同的函数. ·Get All Map Layers - 这个按钮调用getMapLayer ...

  2. libvirt 部分API 介绍

    感谢朋友支持本博客.欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  3. UI_UISegmentedControl 控件

    创建控件 - (void)createSegmentControl { UISegmentedControl *segmentedControl = [[UISegmentedControl allo ...

  4. Launcher3实现壁纸居中

    Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,仅仅能确保第一次开机时壁纸居中,后面用户改动桌面数目后,就无法达到了.怎样要在默认桌面数目配置时居中 ...

  5. AES加密解密在JAVA和ANDROID下互通

    <span style="font-family: Arial, Helvetica, sans-serif;">昨天外包安卓的那个人说AES的加解密结果不一样.于是百 ...

  6. FPGA设计中的电源管理(转载)

    过去,FPGA设计者主要关心时序和面积使用率问题.但随着FPGA不断取代ASSP和ASIC器件,设计者们现正期望能够开发低功耗设计,在设计流程早期就能对功耗进行正确估算,以及管理和对与FPGA相关的各 ...

  7. url与图片

    http://restapi.amap.com/v3/staticmap?location=116.481485,39.990464&zoom=10&size=750*300& ...

  8. hdoj--2955--Robberies(背包好题)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. Google Summer of Code 2017 经验谈

    Google Summer of Code (GSoC) 2018 又要开始了. 如果想实现你心中的开源梦想, 用代码让世界变得更美好. 参加GSoC可能是你进入开源的世界最好途径. GSoC是什么 ...

  10. 在C# 获取当前应用网址

    /// <summary>        /// 获取当前应用网址        /// </summary>        /// <returns></r ...