大家平时见到的最多的可能就是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>

源代码:点击打开链接

最新文章

  1. 转 用JS实现PHP的sprintf函数
  2. [转]10个学习Android开发的网站推荐
  3. Java 装饰模式 (Decorator)
  4. SVN Cleanup failed的解决办法
  5. Attributes(2): Displaying attributes for a class.(显示类属性)
  6. (总结)Nginx配置文件nginx.conf中文详解 &lt;转&gt;
  7. ajax创建对象
  8. spring 运行时属性值注入
  9. Python内置函数(5)——pow
  10. kettle 在javascrip代码组件中使用fireToDB()函数实现自定义数据库查询
  11. 自学Python4.3-装饰器固定格式
  12. Codeforces | CF1041F 【Ray in the tube】
  13. javaweb开发.页面中文乱码问题
  14. The logback manual #02# Architecture
  15. ZooKeeper 集群的安装部署
  16. django-模板初探
  17. 号称简明实用的django上手教程
  18. SignalR实现在线聊天室功能
  19. C# 多维数组
  20. 【转载】移动开发中的上下左右滑动插件jquery.swipe.js

热门文章

  1. 【UVA 437】The Tower of Babylon(拓扑排序+DP,做法)
  2. Word 操作组件介绍 - Spire.Doc
  3. Flutter入门
  4. intellij idea 13
  5. 变量对象、作用域链和This
  6. 紫书 例题 9-12 UVa 12186 (树形dp)
  7. qt获取磁盘序列号
  8. ArcGIS Engine 9.3启动程序报错
  9. 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
  10. java.lang.IllegalArgumentException: The observer is null.终于解决方式