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

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

最后通过start 方法就可。

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

[java] view plaincopy
  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

[java] view plaincopy
  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>

源代码:点击打开链接

转载于:https://www.cnblogs.com/Free-Thinker/p/6721791.html

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

  1. Android学习笔记26:图片切换控件ImageSwitcher的使用

    在Windows操作系统中,要查看多张图片,可以通过使用"Windows照片查看器"在"上一张"和"下一张"之间切换,进行多张图片的浏览. ...

  2. Android学习笔记之在图片特效

    1.涂鸦(能清屏) HandWritingActivity.java [java] view plaincopy package xiaosi.handWriting; import android. ...

  3. Android学习笔记——用ObjectAnimator平移动画仿微信表情雨的简单实现

    最近需要实现类似微信聊天界面输入文字命中关键字出现Emoji 表情雨的动画效果,自己试着实现了一下,主要就是用 ObjectAnimatior 的 平移 动画,用随机函数控制每个Emoji 的 横,纵 ...

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

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

  5. android给图片加边框,Android学习笔记进阶19之给图片加边框

    //设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...

  6. Android学习笔记(十一):Activity-ListView

    在手机中经常有列表方式.如果Activity中只有唯一一个List(这也是通常的情况),可以继承ListActivity来实现.我们用两个例子来学习List. List例子一:利用Android自带的 ...

  7. Android学习笔记进阶十三获得本地全部照片

    这是Intent的一个用法. 在ActivityAction里面有一个"ACTION_GET_CONTENT"字符串常量,该常量让用户选择特定类型的数据. intent.setTy ...

  8. Android学习笔记19-ImageView实现图片适屏与裁剪

    在Android中,要将一张图片显示在屏幕上,需要创建一个显示图片的对象,该对象就是ImageView. 1.ImageView常用属性 要对图片进行适屏裁剪操作,首先需要了解ImageView的常用 ...

  9. Android学习笔记(十一)——将Fragment添加到Activity中以及参数传递

    将Fragment添加到Activity中 Fragment的布局文件 <LinearLayout xmlns:android="http://schemas.android.com/ ...

最新文章

  1. DWS和各异构数据库的差异对比
  2. 利用CSS定位背景图片
  3. Server-Side UI Automation Provider - WinForm Sample
  4. Webkit for android
  5. LeetCode【3--无重复的最长字串】 LeetCode【4--有序数组中的中位数】
  6. Selenium 自动化测试基础知识
  7. joblib多线程、多进程学习案例(一)——一步步写多进程任务
  8. Swift教程之基本操作符
  9. iOS 使用NSUserdefault 保存自定义的 对象
  10. android 通讯录恢复,通讯录恢复
  11. excel甘特图制作项目进度管理表
  12. Linux 运行.exe程序
  13. YOLO系列梳理(九)初尝新鲜出炉的YOLOv6
  14. Android实现随意拖动View效果
  15. 使用H5开发移动端app
  16. steam版拳皇14无法正常启动
  17. android水电工具softonic,Softonic
  18. esp8266 NVS_FLASH 应用指南
  19. 解决国内手机安装Google Play打开闪退问题
  20. Java8 Lambda表达式(三)Lambda表达式与Stream API

热门文章

  1. java 外部类似_[求指点] 如何用java 实现类似linux中管道调用外部程序的功能
  2. java 对象内存布局_Java--对象内存布局
  3. mysql数据库验证登陆不上_MySQL数据库连接不上、密码修改问题
  4. html 右边框变短,HTML / CSS:使边框右侧高度动态化
  5. 学校计算机机房好处,浅谈学校计算机机房维护
  6. π是无理数证明定积分_证明圆周率是无理数很容易?人类花了2000年!
  7. app获取个人信息是否合法_【关注】如何界定App违法违规收集个人信息?认定方法来了!...
  8. usb禁止重定向_远程桌面中的USB重定向技术解析(功能篇)
  9. datagrid表头与内容不对应_斜线表头竟然如此简单,花上3分钟学习,从此告别低颜值报表...
  10. k8s mysql operator_将 MySQL 通过 presslabs/mysql-operator 部署到 k8s 内部