Android 用Animation-list实现逐帧动画

我们要实现的效果

这里写图片描述

准备资源文件放到res/drawable文件下中

这里写图片描述
light01.png
这里写图片描述
light02.png
这里写图片描述
light03.png

编写loading.xml 放到res/drawable

<!--   
    animation-list :根标签
    oneshot :代表着是否只展示一遍,设置为false会不停的循环播放动画  
    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/light01"
        android:duration="50"/>
    <item
        android:drawable="@drawable/light02"
        android:duration="50"/>
    <item
        android:drawable="@drawable/light03"
        android:duration="50"/>

</animation-list>

activity_main.xml布局文件中,这样写

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">

    <ImageView android:id="@+id/main_img_loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:src="@drawable/loading"/> 

    <Button android:id="@+id/main_bt_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:text="开始" />

    <Button android:id="@+id/main_bt_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:text="停止" />

</LinearLayout>

在Activity中这样来写

bt_start.setOnClickListener(new OnClickListener()  
    {  
    @Override  
    public void onClick(View v) {  
    //获取 AnimationDrawable
        animationDrawable = (AnimationDrawable) animationIV.getDrawable();  
        //开始动画
        animationDrawable.start();  
    }  

});   

bt_stop.setOnClickListener(new OnClickListener()  
{  
    @Override  
    public void onClick(View v) { 
    //获取 AnimationDrawable  
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        //停止动画 
        animationDrawable.stop();  
    }  

});  

有兴趣的童鞋可以关注我的Blog,我的专栏会持续更新Android Studio 权威教程,以及2015 I/O大会上的NDK的配置和编译,我也全部会分享给大家。
并且我收到了CSND 的讲师邀请,后期我会把这些Android Studio的使用教程录制成视频。


/**
 * --------------
 * 欢迎转载   |  转载请注明
 * --------------
 * 如果对你有帮助,请点击|顶|
 * --------------
 * 请保持谦逊 | 你会走的更远
 * --------------
 * @author zsl
 * @github https://github.com/yy1300326388
 * @blog http://blog.csdn.net/yy1300326388
 */
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZeroFlutter

感谢支持,请我喝杯咖啡

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

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

打赏作者

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

抵扣说明:

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

余额充值