您现在的位置是:首页 > 文章详情

Android动画之帧动画详解

日期:2018-07-24点击:612

一.概念

帧动画也叫Drawable Animation,是最简单最直观的动画类型,他利用人眼的视觉暂留效应——也就是光对视网膜所产生的视觉在光停止作用后,仍会保留一段时间的现象。在Android中实现帧动画,就是由设计师给出一系列状态不断变化的图片,开发者可以指定动画中每一帧对应的图片和持续时间,然后就可以开始播放动画了。具体有两种方式可以实现帧动画,分别是采用XML资源文件和代码实现。

二.实现

◆XML资源文件方式

1.在res/drawable目录中放入需要的图片

img_9f3fbfb84b4282c4ce06559bfad019a1.png

2.在res/drawable目录中新建animlist.xml文件,其中oneshot表示是否循环播放false为循环播放,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/a1"
        android:duration="120"/>
    <item android:drawable="@drawable/a2"
        android:duration="120"/>
    <item android:drawable="@drawable/a3"
        android:duration="120"/>
    <item android:drawable="@drawable/a4"
        android:duration="120"/>
    <item android:drawable="@drawable/a5"
        android:duration="120"/>
    <item android:drawable="@drawable/a6"
        android:duration="120"/>
    <item android:drawable="@drawable/a7"
        android:duration="120"/>
</animation-list>

3.在布局文件中进行设置

<ImageView
    android:id="@+id/image"
    android:background="@drawable/animlist"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:layout_height="wrap_content" />

4.代码中播放动画

AnimationDrawable animationDrawable= (AnimationDrawable) imageView.getBackground();
//开始动画
animationDrawable.start();
//结束动画
animationDrawable.stop();

◆代码动态实现

AnimationDrawable animationDrawable=new AnimationDrawable();
    for(int x=1;x<8;x++){
        int id=getResources().getIdentifier("a"+x,"drawable",getPackageName());
        Drawable drawable=getResources().getDrawable(id);
        animationDrawable.addFrame(drawable,120);
    }
//设置是否循环播放
 animationDrawable.setOneShot(false);
 imageView.setBackgroundDrawable(animationDrawable);
//开始动画
 animationDrawable.start();
//结束动画
animationDrawable.stop();

效果展示

img_81a7b9a8db3c7437f57920bfceb03e60.gif
image

个人博客:https://myml666.github.io/

原文链接:https://yq.aliyun.com/articles/665571
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章