博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android播放图片动画
阅读量:4580 次
发布时间:2019-06-09

本文共 2064 字,大约阅读时间需要 6 分钟。

1、布局文件中添加ImageView

<ImageView

android:id="@+id/iv_fan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f1" />

//注意:如果不是手写的代码,即拖动进布局的ImageView需要修改android:src   改成android:background,否则无法播放!

2、res文件夹下添加anim文件夹,用来存放<animation-list>

  在anim文件夹下创建一个名称为fan_list的xml文件

  右键anim文件夹->new->Android XML File进入选择界面

  Resource Type: 选择Drawable

  Project: 自己的工程目录,通常情况下程序自己已经匹配好了,不用配置

  File:   xml文件的名称

  Root Element:  选择Animation-list  

  选择Finish 完成创建

3、创建完成文件会出现在Drawable文件夹下,没关系,复制粘贴到anim文件夹下即可

  编辑xml文件,创建好的xml文件通常只带有一个头文件和一个标签

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

//在此处填写数据

</animation-list>

 

 

  填写数据的时候,android不会给出提示,所以需要记住

  fan_list文件内容如下:

  <?xml version="1.0" encoding="utf-8"?>

  <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item

  android:drawable="@drawable/f1"
  android:duration="100"/>
  <item
  android:drawable="@drawable/f2"
  android:duration="100"/>
  <item
  android:drawable="@drawable/f3"
  android:duration="100"/>
  <item
  android:drawable="@drawable/f4"
  android:duration="100"/>
  <item
  android:drawable="@drawable/f5"
  android:duration="100"/>
  <item
  android:drawable="@drawable/f6"
  android:duration="100"/>
  <item
  android:drawable="@drawable/f7"
  android:duration="100"/>
  <item
android:drawable="@drawable/f8"
android:duration="100"/>

</animation-list>

    其中android:drawable是需要播放的图片数据,程序播放的时候会从上到下依次播放

    android:duration 持续时间,每张图片显示的时间,单位是ms

    这两个属性必须填写,不然无法播放

4、在程序中引用

public void InitView(){

  iv_fan = (ImageView) findViewById(R.id.imageView1);//实例化控件

  iv_fan.setBackgroundResource(R.anim.fan_list);//设置播放数据
  final AnimationDrawable anim = (AnimationDrawable) iv_fan//使用AnimationDrawable控制数据的播放和暂停
  .getBackground();
  iv_fan.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

      anim.setOneShot(true);//设置只播放一次,false无限循环播放

      anim.start();//播放

    }

  });

}

 

转载于:https://www.cnblogs.com/MainActivity/p/6016619.html

你可能感兴趣的文章
如何辞职
查看>>
SSO 单点登录总结(PHP)
查看>>
Ubuntu16.04下将hadoop2.7.3源代码导入到eclipse neon中
查看>>
朝令夕改的企业不值得留恋
查看>>
springboot踩坑出坑记
查看>>
Cross Entropy in Machine Learning
查看>>
Myslq 之登陆、退出
查看>>
ovs源码阅读--netlink使用
查看>>
php中引用&的真正理解-变量引用、函数引用、对象引用
查看>>
cmake编译安装mysql 5.6.12
查看>>
第七章学习小结
查看>>
GS LiveMgr心跳管理类
查看>>
设计模式学习笔记(二)之观察者模式、装饰者模式
查看>>
mysql导出数据库和恢复数据库代码
查看>>
走出软件泥潭 第一回 雪上加霜
查看>>
小鸟哥哥博客 For SAE
查看>>
gui编程实践(3)--记事本界面 JMenuBar JMenu
查看>>
App测试方法总结
查看>>
51nod-1228: 序列求和
查看>>
BZOJ1303: [CQOI2009]中位数图
查看>>