Drawable Animation

一、实现方式

(1)代码中添加实现

  • 将图片添加到AnimationDrawable对象,在将对象添加到imageView中,调用start()方法启动动画。
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.drawable.a), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.b), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.c), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.d), 200);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.e), 200);
animationDrawable.setOneShot(true);//设置是否只播放一次
imageView.setImageDrawable(animationDrawable);
animationDrawable.start();

(2)生成 animation-list 的资源文件,在代码中引用。

  • 在drawable文件下新建abunation_list的xml文件,在文件中添加图片资源。
  • 将xml文件添加到imageView中,通过getDrawable方法获取AnimationDrawable对象,调用start()方法启动动画。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item
        android:drawable="@drawable/a"
        android:duration="1000" />
    <item
        android:drawable="@drawable/b"
        android:duration="1000" />
    <item
        android:drawable="@drawable/c"
        android:duration="1000" />
    <item
        android:drawable="@drawable/d"
        android:duration="1000" />
    <item
        android:drawable="@drawable/e"
        android:duration="1000" />
</animation-list>
  • oneshot:设置是否只播放一次,默认为false
  • drawable:设置当前图片资源
  • duration:设置当前图片时间
imageView.setBackgroundResource(R.drawable.anim_list);
animationDrawable = (AnimationDrawable) imageView.getDrawable();
animationDrawable.start();

注意方式:

  • 在onResume方法中添加它,防止发生空指针异常。
  • 不建议添加太大的图片,因为这很容易导致OOM。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,699评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 我发现,我自己觉得写得好的日记,在简书上没啥反应,而我认为写得很一般的日记,却有不少关注量,有人喜欢,有人点赞,然...
    土川兄一终身建设阅读 1,068评论 0 8
  • 苏苏养了一只猫,一只公猫,一只灰色的狸花猫。这只公猫尤为含蓄,苏苏xiangle
    ZQ郑阅读 169评论 0 1
  • 作者:美丽 去朋友家做客 进入院子 扑鼻而来的是甜甜的味道 是我喜欢的味道 是很久很久没有闻到的槐花味 小时候常常...
    雪花琵琶阅读 623评论 7 7