分享

浅谈Android中AnimationDrawable使用

 爱开发 2022-08-02 发布于广东

如果你想第一时间收到类似的文章

点击上面↑「爱开发」关注我们!

今天分享的内容比较简单,就是一个简单加载效果

AnimationDrawable是实现Drawable Animation的基本类

XML文件存放在工程中res/drawable目录下,XML文件的指令(属性)为动画播放的顺序和时间间隔。

 在XML文件中<animation-list>元素为根节点,<item>节点定义了每一帧,表示一个drawable资源的帧和帧间隔。

和往常一样,我们先来看效果,不过今天的效果特别简单。

我们在项目res/drawable 路径下load.xml

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

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

    android:oneshot="false">

    <!-- item 放图片  设置图片显示时间 -->

    <item

        android:drawable="@drawable/amp_land_1"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_2"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_3"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_4"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_5"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_6"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_7"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_8"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_9"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_10"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_11"

        android:duration="100" />

    <item

        android:drawable="@drawable/amp_land_12"

        android:duration="100" />

</animation-list>

需要留意的是:当Android:oneshot属性为true时,表示此动画只执行一次,最后停留在最后一帧。设置为false则表示动画循环播放。以前项目中有个展示关于航班飞机状态的问题,其实设置按照这种方法设置oneshot就好,没那么复杂。

<ImageView

    android:id="@+id/iv_load"

    android:layout_width="match_parent"

    android:layout_height="30dp"

    android:layout_centerInParent="true"

    android:background="#00000000"

    android:src="@drawable/load"

    android:scaleType="center" />

<TextView

    android:id="@+id/top"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:gravity="center"

    android:text="加载中" />

ImageView imageView = (ImageView)this.findViewById(R.id.iv_load);

AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();

animationDrawable.start();

Drawable Animation本身就是一个Drawable资源文件,所以直接在xml中设置为指定View的背景即可。animation.start()

同样可以通过代码设置View. setBackgroundResource(resID)   animation.start()

作者:洪生鹏,白天到工地搬砖,晚上写故事,毕业七年,着重写有温度又有深度的职场感悟。公众号:爱开发(aikaifa),记得认清楚ID是 aikaifa 转载请联系授权。

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多