Android 基础动画之帧动画详解
在上一篇 Android 基础动画之补间动画详解 主要介绍了补间动画的基本属性以及补间动画在Android起始设计之初带来的一些问题。本篇文章主要介绍的是Android的帧动画。
帧动画,我们从字面意思来理解,帧:就是影像动画中最小单位的单幅影像画面,相当于电影胶片上的每一格镜头。 一帧就是一副静止的画面,连续的帧就形成动画,如电视图象等。简单点说就是类似幻灯片播放的那种效果,因此帧动画的本质就是将一张张的图片,通过代码对这些图片进行连续的活动(这样就形成了动画)
假设现在有这样一个效果如图,这个动画就类似手机WIFI连接信号,从一格到满格
那么,使用帧动画的方式该如何实现该效果?
-
xml方式实现
文件名:anim_list.xml (文件存放路径: main/res/drawable)
anim_list.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="@drawable/anim_1" android:duration="200"/> <item android:drawable="@drawable/anim_2" android:duration="200"/> <item android:drawable="@drawable/anim_3" android:duration="200"/> <item android:drawable="@drawable/anim_4" android:duration="200"/> </animation-list>
布局
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/anim_list"/>
获取背景
//获取背景,并将其强转成AnimationDrawable AnimationDrawable animationDrawable = (AnimationDrawable) iv.getBackground(); //判断是否在运行 if(!animationDrawable.isRunning()){ //开启帧动画 animationDrawable.start(); }
以上是通过XML方式完成帧动画,下面是通过代码方式完成帧动画
-
代码方式实现
//创建一个AnimationDrawable AnimationDrawable animationDrawable1 = new AnimationDrawable(); //准备好资源图片 int[] ids = {R.drawable.anim_1,R.drawable.anim_2,R.drawable.anim_3,R.drawable.anim_4}; //通过for循环添加每一帧动画 for(int i = 0 ; i < 4 ; i ++){ Drawable frame = getResources().getDrawable(ids[i]); //设定时长 animationDrawable1.addFrame(frame,200); } animationDrawable1.setOneShot(false); //将动画设置到背景上 iv.setBackground(animationDrawable1); //开启帧动画 animationDrawable1.start();
关于帧动画使用XML形式编写值得一提的是:<animation-list>必须是根节点,这个根节点包含一个或者多个<item>元素,<item>简单理解就是类似一帧的动画资源。item属性有:
android:oneshot属性,其中true代表只执行一次,false循环执行;android:drawable ,代表一个frame的Drawable资源;android:duration,代表一个frame显示多长时间。
值得一提的是:AnimationDrawable的start()方法不能在Activity的onCreate方法中调用,因为AnimationDrawable有可能在加载的时候还没有完全加载到window上,所以最好的使用时机是onWindowFocusChanged()方法中。
下面在给大家举个帧动画的例子,下面是4张资源图:
- 首先,在drawable文件夹下,新建一个 loading.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="@drawable/progress_loading_image_01" android:duration="80"/> <item android:drawable="@drawable/progress_loading_image_02" android:duration="80"/> <item android:drawable="@drawable/progress_loading_image_03" android:duration="80"/> <item android:drawable="@drawable/progress_loading_image_04" android:duration="80"/> </animation-list>
- 接着,布局XML文件中引用该 loading.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" > <ImageView android:layout_gravity="center" android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/loading"/> <Button android:layout_marginTop="20dp" android:layout_marginRight="80dp" android:layout_marginLeft="80dp" android:text="跑跑更健康" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout>
- 最后,解析XML,并开启动画:
public class AnimActivity extends BaseActivity{ @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_anim); ImageView iv = findViewById(R.id.iv); //获取背景,并将其强转成AnimationDrawable AnimationDrawable animationDrawable = (AnimationDrawable) iv.getBackground(); //判断是否在运行 if(!animationDrawable.isRunning()){ //开启帧动画 animationDrawable.start(); } } }
-
最终效果如下:
关于帧动画的基本介绍和两种使用就介绍完毕了。
如果这篇文章对你有帮助,希望各位看官留下宝贵的star,谢谢。
Ps:著作权归作者所有,转载请注明作者, 商业转载请联系作者获得授权,非商业转载请注明出处(开头或结尾请添加转载出处,添加原文url地址),文章请勿滥用,也希望大家尊重笔者的劳动成果
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Airbnb: React Native 从选择到放弃
Airbnb 最近在 Medium 上发布了一系列文章详细描述了 Airbnb 与 React Native 从选择到放弃的整个心路历程。 React Native at Airbnb The Technology Building a Cross-Platform Mobile Team Making a Decision on React Native What's Next for Mobile 对于字多不看的同学,可以简单看一下我下面的小结。 当初为什么选择 React Native 有限的开发团队满足不了日益增长的业务需求 对 React Native 的期望 快速开发 质量有保证 一次编写,多平台共享 提升开发体验 我们所怀念的 跨平台,实际上有 95% 以上的共享代码率。 统一的 DSL。根据平台也做具体的差异化实现。 React 是个好东西。组件化,简单的生命周期,声明式 开发迭代速度(热更新 hot-reloading) 我们在 RN 生态基础设施上的投资。 性能,在绝大部分页面上 RN 都表现得很流畅。(有性能问题? shouldComponentUpdate, r...
- 下一篇
linux中btt工具详解
在之前的文章中介绍了如何使用blktrace 以及其工作原理和架构。我们知道blktrace 跟踪块设备的统计信息,每个CPU会有一个文件存储,然后通过blkparse可以将这些文件整合成一个文件来显示。 不过blkparse显示的文件过于庞大,而通过btt分析后会发现监控数据变得更加有意义。 1. 示例 监控获得数据 #blktrace -d /dev/sda -o sdatest 合并多个文件 #blkparse -i sdatest -d sdatest.bin #btt -i sdatest.bin 2. 输出简要信息 第一部分平均时间 IO时间主要是分为三个区域: nÂÂ 插入或合并IO到请求队列的时间,计算从块层到插入,即Q2I(Q2I=Q2G+G2I) nÂÂ 请求队列到驱动的时间,即是I2D。 nÂÂ 驱动和设备时间,即是D2C. 此外还有还有Q2Q表示IO交到块层的时间。系统等待请求的时间在Q2G中。一般情况下Q2C=Q2I+I2D+D2C,说一般情况因为有些IO会会执行merge。 把大写字母函数的表,再次方便查找: Act Description A IO wa...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- 设置Eclipse缩进为4个空格,增强代码规范
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7设置SWAP分区,小内存服务器的救世主