AnimationDrawable设置默认帧

首先定义anim:
res/drawable/chat_right_voice_anim.xml:

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

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@mipmap/icon_send_voice_playing_001" android:duration="120"/>
    <item android:drawable="@mipmap/icon_send_voice_playing_002" android:duration="120"/>
     <item android:drawable="@mipmap/icon_send_voice_playing_003" android:duration="120"/>
</animation-list>

布局文件定义:

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/item_right_voice_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/dd_dp10"
        android:layout_toStartOf="@+id/item_right_head_icon"
        android:layout_centerVertical="true"
        android:gravity="end"
        android:src="@drawable/chat_right_voice_anim"/>

java文件定义:

private AnimationDrawable anim;
private ImageView voiceImg;

private void test(){
 anim = (AnimationDrawable)voiceImg.getDrawable();
}

/**
* 设置默认帧数
**.
private void setDefaultFrame(){
anim.selectDrawable(0);
}

注意:

设置初始默认帧数时调用selectDrawable(int index)无效,需要间隔一段时间,所以初始默认帧数需要在xml文件中定义成第一帧才行,如下:
res/drawable/chat_right_voice_anim.xml:

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

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
     <item android:drawable="@mipmap/icon_send_voice_playing_003" android:duration="120"/>
    <item android:drawable="@mipmap/icon_send_voice_playing_001" android:duration="120"/>
    <item android:drawable="@mipmap/icon_send_voice_playing_002" android:duration="120"/>

</animation-list>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AnimationDrawable是一个用于实现动画的类。它允许您将多张图片以特定的顺序显示在屏幕上,从而创建连续的动画效果。 要使用AnimationDrawable,首先需要在XML或Java代码中定义一个AnimationDrawable对象,并将每一图片添加到其中。然后,您可以通过调用start()方法启动动画,或者通过调用stop()方法停止动画。 以下是一个使用AnimationDrawable创建动画的示例: 1. 在XML文件中定义AnimationDrawable对象和每一图片: ```xml <animation-list android:id="@+id/animation" android:oneshot="false"> <item android:drawable="@drawable/frame1" android:duration="100"/> <item android:drawable="@drawable/frame2" android:duration="100"/> <item android:drawable="@drawable/frame3" android:duration="100"/> <!-- 添加更多的图片 --> </animation-list> ``` 2. 在Java代码中获取AnimationDrawable对象,并将其应用到ImageView上: ```java ImageView imageView = findViewById(R.id.imageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); ``` 3. 启动动画: ```java animationDrawable.start(); ``` 这样,您就可以在ImageView上显示动画了。每一图片将按照指定的持续间进行切换,从而呈现出动画效果。 请注意,AnimationDrawable可以通过调用stop()方法停止动画。此外,您还可以设置动画是否循环播放,通过将android:oneshot属性设置为true或false来实现。 希望这个例子能够帮助您理解如何使用AnimationDrawable创建动画!如果有任何问题,请随提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值