Android逐帧动画和补间动画

本篇博客来看一下Android中的逐帧动画和补间动画。


一、逐帧动画
逐帧动画也叫Drawable Animation。
在Android中实现逐帧动画,就是由设计师给出一系列状态不断变化的图片,
开发者可以指定动画中每一帧对应的图片和持续的时间,然后在合适的时候播发动画。

最常用定义逐帧动画的方式是:
在res/drawable目录下,放置动画对应的图片,并定义animation.xml文件。
animation.xml的定义类似于:

<?xml version="1.0" encoding="utf-8" ?>
<!--oneshot表示是否重复播放动画, true表示只播放一次, false表示重复播放 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">

    <!--drawable指定图片, duration表示图像的持续时间-->
    <item
        android:drawable= "@drawable/p_1"
        android:duration="1200"/>

    <item
        android:drawable= "@drawable/p_2"
        android:duration="1200"/>

    <item
        android:drawable= "@drawable/p_3"
        android:duration="1200"/>
</animation-list>

负责播放动画的View,需要在xml中配置src属性为animation.xml,例如:

<ImageView
    android:id="@+id/test_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/animation" />

然后就可以在代码中,启动动画了:

ImageView imageView = findViewById(R.id.test_view);

//获取AnimationDrawable
AnimationDrawable drawable = (AnimationDrawable)imageView.getDrawable();

//调用start接口开始播放
//AnimationDrawable还有其它接口,例如停止、增加帧等
drawable.start();

如果不定义animation.xml文件,也可以仅通过代码实现逐帧动画:

        //在Activity中直接getDrawable,需要API >= 21
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AnimationDrawable animationDrawable = new AnimationDrawable();

            int[] ids = new int[] {R.drawable.p_1, R.drawable.p_2, R.drawable.p_3};

            for (int id : ids) {
                Drawable tmp = getDrawable(id);
                if (tmp != null) {
                    //利用addFrame接口, 增加帧
                    animationDrawable.addFrame(tmp, 1200);
                }
            }

            animationDrawable.setOneShot(true);

            ImageView testView = findViewById(R.id.another_test_view);
            //设置animationDrawable
            testView.setBackground(animationDrawable);

            animationDrawable.start();
        }
    }

二、补间动画
补间动画是指开发者无需定义动画过程中的每一帧,
只需要定义动画的开发和结束这两个关键帧&#x

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值