帧动画入门

第一种,应用动画文件到背景:

文件可以放到drawable文件夹,也可以放到anim文件夹

<?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/images_3_0001"
        android:duration="500" />
    <item
        android:drawable="@drawable/images_3_0002"
        android:duration="500" />
    <item
        android:drawable="@drawable/images_3_0003"
        android:duration="500" />
    <item
        android:drawable="@drawable/images_3_0004"
        android:duration="500" />
    <item
        android:drawable="@drawable/images_3_0005"
        android:duration="500" />
    <item
        android:drawable="@drawable/images_3_0006"
        android:duration="500" />
</animation-list>

其中用到了六幅图片。

布局就是一个ImageView。

代码:

/**
     * 方案1:xml文件,background
     */
    private void scheme1() {
        imgAnimationContainer.setBackgroundResource(R.drawable.frame_animation_1);
        AnimationDrawable anim = (AnimationDrawable) imgAnimationContainer.getBackground();
        anim.start();
    }

第二种,应用动画文件到drawable

代码:

 /**
     * 方案2:xml文件,drawable
     */
    private void scheme2() {
        imgAnimationContainer.setImageResource(R.drawable.frame_animation_1);
        AnimationDrawable anim = (AnimationDrawable) imgAnimationContainer.getDrawable();
        anim.start();
    }
第三种,纯代码实现

/**
     * 方案3:纯代码
     */
    private void scheme3() {
        int[] imagesRes = {R.drawable.images_3_0001,
                R.drawable.images_3_0002,
                R.drawable.images_3_0003,
                R.drawable.images_3_0004,
                R.drawable.images_3_0005,
                R.drawable.images_3_0006
        };
        AnimationDrawable animation = new AnimationDrawable();
        for (int i = 0; i < 6; i++) {
            Drawable drawable = getResources().getDrawable(imagesRes[i]);
            animation.addFrame(drawable, 1000);
        }
        animation.setOneShot(false);//不断循环
        imgAnimationContainer.setImageDrawable(animation);
        animation.start();
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值