Android动画之帧动画

Android动画之帧动画

帧动画,顾名思义就是一帧一帧的动画,就是把图片一张一张的按顺序的展示出来。需要用到的api是AnimationDrawable。

实现方法1

把图片放入一个数组中,然后利用AnimationDrawable循环遍历即可。

//这里把图片放入数组中
public static final int[] ids_index = {R.drawable.ic_icon_index_svg_1,R.drawable.ic_icon_index_svg_2,R.drawable.ic_icon_index_svg_3,R.drawable.ic_icon_index_svg_4
            ,R.drawable.ic_icon_index_svg_5,R.drawable.ic_icon_index_svg_6,R.drawable.ic_icon_index_svg_7,R.drawable.ic_icon_index_svg_8
            ,R.drawable.ic_icon_index_svg_9,R.drawable.ic_icon_index_svg_10,R.drawable.ic_icon_index_svg_11,R.drawable.ic_icon_index_svg_12
            ,R.drawable.ic_icon_index_svg_13,R.drawable.ic_icon_index_svg_14,R.drawable.ic_icon_index_svg_15,R.drawable.ic_icon_index_svg_16
            ,R.drawable.ic_icon_index_svg_17,R.drawable.ic_icon_index_svg_18,R.drawable.ic_icon_index_svg_19};

//具体帧动画的实现
//创建一个AnimationDrawable
        AnimationDrawable animationDrawable1 = new AnimationDrawable();

        //通过for循环添加每一帧动画
        for(int i = 0 ; i < ids_index.length ; i ++){
            Drawable frame = context.getResources().getDrawable(ids_index[i]);
            //设定时长
            animationDrawable1.addFrame(frame,10);
        }
        //true代表动画只执行一次
        animationDrawable1.setOneShot(true);
        //将动画设置到背景上
        iv_index_icon.setImageDrawable(animationDrawable1);
        //开启帧动画
        animationDrawable1.start();
实现方法2

在drawable下新建一个animation-list,把要动画的每一帧图片放进去,然后把这个drawable放入要显示动画的控件中,最后利用AnimationDrawable 实现动画即可。

    AnimationDrawable animationDrawable = (AnimationDrawable) iv_index_icon.getDrawable();
        //开启帧动画
        animationDrawable.start();
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/ic_icon_index_svg_1" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_2" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_3" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_4" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_5" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_6" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_7" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_8" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_9" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_10" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_11" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_12" android:duration="10"/>
    <item android:drawable="@drawable/ic_icon_index_svg_13" android:duration="10"/>


</animation-list>
上一篇:【工程化】icon图标的获取


下一篇:C++ 类中的const关键字