java控制一套动效图播放、暂停和重置的方法

UI 出一套动效图一般来说有25张左右,如何控制这些图片自动的依次循环播放且能随时暂停和重置呢?我在看别人代码时看到了这样一个方法:


1.在 drawable 目录下新建一个animation-list,名字任取,比如说命名为play_btn_animation_list,把图片资源放入循环列表,从第一张到第二十五张,其中android:oneshot="false"表示循环播放,若设置为true则只播放一次,不会循环播放,android:duration用来指定每一帧的播放持续时间;


<?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/recorder_anmation_1"
        android:duration="20">
    </item>
    <item
        android:drawable="@drawable/recorder_anmation_2"
        android:duration="20">
    </item>

中间图片省略......

    <item
        android:drawable="@drawable/recorder_anmation_24"
        android:duration="20">
    </item>
    <item
        android:drawable="@drawable/recorder_anmation_25"
        android:duration="20">
    </item>

</animation-list>

2.在Activity新建一个AnimationDrawable对象:

private AnimationDrawable animationDrawable;

3.初始化 AnimationDrawable 对象和用来显示该动画的ImageView控件并使其显示循环动画图片集合中的第一张图:

mStart_bg_state = (ImageView) findViewById(R.id.play_btn_state);
mStart_bg_state.setBackground(getResources().getDrawable(R.drawable.play_btn_animation_list));
animationDrawable = (AnimationDrawable) mStart_bg_state.getBackground();

4.开始依次播放:

animationDrawable.start();

5.暂停播放:

animationDrawable.stop();

6.重置,恢复显示第一张:

mStart_bg_state.setBackground(getResources().getDrawable(R.drawable.play_btn_animation_list));
animationDrawable = (AnimationDrawable) mStart_bg_state.getBackground();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

却染人间愁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值