Android中帧动画的实现

1.Android中动画分类

Android中的动画分为3大类:帧动画,补间动画和属性动画

2.帧动画实现方式

帧动画由动画图形AnimationDrawable生成

3.AnimationDrawable常用方法

  • addFrame:添加一幅图片帧,并指定该帧的持续时间(单位毫秒)。
  • setOneShot:设置是否只播放一次。为true表示只播放一次,为false表示循环播放。
  • start:开始播放。注意,设置宿主视图后才能进行播放。
  • stop:停止播放。
  • isRunning:判断是否正在播放。

4.帧动画显示方案

有了动画图形,还得有一个宿主视图显示该图形,一般使用图形视图ImageView承载AnimationDrawable,即调用ImageView对象的setImageDrawable方法将动画图形加载到图像视图中。

5.代码中实现帧动画

    private void showFrameAnimByCode(){
        ad_frame = new AnimationDrawable();
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p1),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p2),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p3),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p4),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p5),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p6),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p7),50);
        ad_frame.addFrame(getResources().getDrawable(R.mipmap.flow_p8),50);
        ad_frame.setOneShot(false);
        iv_pic.setImageDrawable(ad_frame);
        ad_frame.start();
    }

6.资源文件中实现帧动画

在drawable下创建资源文件frame_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/flow_p1"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p2"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p3"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p4"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p5"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p6"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p7"
        android:duration="50" />
    <item
        android:drawable="@mipmap/flow_p8"
        android:duration="50" />
</animation-list>

在代码中

    private void showFrameAnimByCode(){
        iv_pic.setImageResource(R.drawable.frame_anim);
        ad_frame = (AnimationDrawable) iv_pic.getDrawable();
        ad_frame.start();
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值