提到Android动画,想必就要说来话长了!在Android系统中,谷歌提供了大量的动画Api来满足开发者产品中各式各样的动画需求。从Android 2.0时期的View动画到Android3.0时期的属性动画,再到Android5.0之后引入的转场动画以及后来为了能够让开发者快捷的实现弹性动画在support 25中引入的SpringAnimation等等,各式各样的动画框架层出不穷。对于初学者来说,见到炫酷的动画特效时直呼牛批,但让自己动手写动画时面对众多的动画却是一脸懵逼。因此本篇文章将对Android中的动画进行一个详尽的梳理,让我们对Android中的动画能有一个系统的认识。下面是整理出来的一个Android动画的思维图,我们将从这些方面来详细了解Android中的动画。
一、视图动画(View Animation)
视图动画又称View动画,视图动画可以分为帧动画(Frame Animation)和补间动画(Tween Animation)两类。本节中将详细介绍这两种动画。
1.帧动画(Frame Animation)
帧动画顾名思义就是逐帧播放的动画。帧动画的实现非常简单,只需要通过编写drawable的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_1" android:duration="200"/>
<item android:drawable="@drawable/progress_2" android:duration="200"/>
<item android:drawable="@drawable/progress_3" android:duration="200"/>
<item android:drawable="@drawable/progress_4" android:duration="200"/>
<item android:drawable="@drawable/progress_5" android:duration="200"/>
<item android:drawable="@drawable/progress_6" android:duration="200"/>
<item android:drawable="@drawable/progress_7" android:duration="200"/>
<item android:drawable="@drawable/progress_8" android:duration="200"/>
</animation-list>
在上面的xml中添加了8帧图片,Android会将其作为一个drawable文件,因此我们可以直接在ImageView的background或src中引用。接下来就可以通过ImageView获取到该drawable并实现动画的控制。代码如下:
ImageView imageView = mDialogView.findViewById(R.id.loadingImageView);
// 这里要注意ImageView设置的时background还是src,如果是background则调用getBackground,否则调用getDrawable().
animationDrawable = (AnimationDrawable) imageView.getBackground();
// 执行帧动画
animationDrawable.start();
// 停止帧动画播放
animationDrawable.stop();
帧动画可以说相当简单,但是不推荐过多使用帧动