美文网首页
Drawable Animation

Drawable Animation

作者: kjy_112233 | 来源:发表于2017-09-15 11:58 被阅读0次

    一、实现方式

    (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。

    相关文章

      网友评论

          本文标题:Drawable Animation

          本文链接:https://www.haomeiwen.com/subject/itessxtx.html