动画是Android一个相当重要的组成部分,作为安卓开发者也许多多少少用过一些动画三方库。我一直想要系统的整理一下动画相关的知识。在网上逛了逛,发现很多都已经总结过了,启航的自定义控件三部曲是他花费大心思写的。真心很不错,鸿洋也写过类似动画的文章,郭神也发过动画介绍,这里我就从头开始,参考着他们的和官方文章学习,这里我只是做一下学习笔记。谷歌官方动画文档:https://developer.android.com/guide/topics/resources/animation-resource.html
动画基本介绍
安卓提供的动画主要分为两种:Property Animation和View Animation。而View Animation又分为TweenAnimation和FrameAnimation。这些都是安卓提供的原生动画类。那么接下来挨着学习,怎么使用。
Animation
Animation共有属性,介绍一下经常用的。Animation类是所有动画(scale、alpha、translate、rotate)的基类。
android:duration 动画持续时间,以毫秒为单位
android:fillAfter 如果设置为true,控件动画结束时,将保持动画最后时的状态
android:repeatCount 重复次数
android:repeatMode 重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
android:interpolator 设定插值器,其实就是指定的动作效果,比如弹跳效果等,不在这小节中讲解,后面会单独列出一单讲解。
View Animation
这里包含了TweenAnimation(补间动画)和FrameAnimation(逐帧动画)。可以使用xml编写,也可以使用类来编写。
Tween Animation
TweenAnimation包含了scale(缩放),translate(平移),alpha(渐变),rotate(旋转)四种动画效果,使用xml设置动画的时候,需要在res目录下创建一个anim文件夹,创建资源文件就行了。类似这样res/anim/filename.xml。这里必须创建anim文件夹用于存放动画资源文件。因为会通过这个文件名作为使用的ID。R.anim.filename。使用xml的时候通过AnimationUtil的loadAnimation导入xml,然后调用目标控件的startAnimation(animation)开启动画。
Scale
scale标签是缩放动画,可以实现动态调控件尺寸的效果,有下面几个属性:
android:fromXScale 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
android:toXScale 结尾的X方向上相对自身的缩放比例,浮点值;
android:fromYScale 起始的Y方向上相对自身的缩放比例,浮点值,
android:toYScale 结尾的Y方向上相对自身的缩放比例,浮点值;
android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。(具体意义,后面会举例演示)
android:pivotY 缩放起点Y轴坐标,取值及意义跟android:pivotX一样。
set标签,包含 alpha, scale, translate, rotate, 或者 set。拥有它自己的属性,android:interpolator 插值器,有很多默认选择,之后会详细介绍插值器。android:shareInterpolator 布尔值,如果设置为true,那么它的子动画会受到该插值器的影响。
接下来实例:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.0"
android:toXScale="1.5"
android:fromYScale="0.0"
android:toYScale="1.5"
android:pivotX="50"
android:pivotY="50"
android:duration="700"
>
</scale>
</set>
在标签属性android:pivotX中有三种取值,数,百分数,百分数p;体现在构造函数中,就是最后一个构造函数的pivotXType,它的取值有三个,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.ABSOLUTE,50f,Animation.ABSOLUTE,50f);
将android:pivotX=”50” android:pivotY=”50”修改为android:pivotX=”50%” android:pivotY=”50%”。就是从控件中心开始动画。
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_SELF,50f,Animation.RELATIVE_TO_SELF,50f);
将android:pivotX=”50%” android:pivotY=”50%”
修改为android:pivotX=”50%p” android:pivotY=”50%p”。就是基于父控件的百分比。
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_PARENT,50f,Animation.RELATIVE_TO_PARENT,50f);
设置重复次数,和重复的模式。
scaleAnimation.setRepeatCount(1);
scaleAnimation.setRepeatMode(Animation.RESTART|Animation.REVERSE);
android:repeatCount=”1”, android:repeatMode=”reverse|restart”
android:repeatMode=”reverse” android:repeatMode=”restart”
Translate
平移动画。xml属性:
android:fromXDelta 起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
android:fromYDelta 起始点Y轴从标,可以是数值、百分数、百分数p 三种样式;
android:toXDelta 结束点X轴坐标
android:toYDelta 结束点Y轴坐标
实例:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="100%"
android:duration="800"
>
</translate>
</set>
java代码:
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE,0.0f,Animation.ABSOLUTE,100f,Animation.ABSOLUTE,0.0f,Animation.ABSOLUTE,100f);
ROTATE
旋转动画,属性:
android:fromDegrees 开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
android:toDegrees 结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
android:pivotY 缩放起点Y轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="800">
</rotate>
</set>
RotateAnimation rotateAnimation = new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,50f,Animation.RELATIVE_TO_SELF,50f);
android:toDegrees=”360” android:toDegrees=”-360”
ALPHA
渐变动画,属性:
android:fromAlpha 动画开始的透明度,从0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明
android:toAlpha 动画结束时的透明度,也是从0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="800">
</alpha>
</set>
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f);
alphaAnimation.setDuration(800);
SET
动画集合,多个动画效果触发。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="2000">
</alpha>
<rotate
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000">
</rotate>
<scale
android:fromXScale="0.0"
android:toXScale="1.5"
android:fromYScale="0.0"
android:toYScale="1.5"
android:pivotY="50%"
android:pivotX="50%"
android:duration="2000">
</scale>
</set>
对应java代码:
AnimationSet animationSet = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f,1.0f);
alphaAnimation.setDuration(2000);
RotateAnimation rotateAnimation = new RotateAnimation(0.0f,720f,Animation.RELATIVE_TO_SELF,50f,Animation.RELATIVE_TO_SELF,50f);
rotateAnimation.setDuration(2000);
ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_SELF,50f,Animation.RELATIVE_TO_SELF,50f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
FrameAnimation
frameAnimation就是逐帧动画,一张一张的播放。就像flash帧动画一样,设置每帧播放时间,连续播放就成了动画。
存放位置:res/drawable/filename.xml 也就是需要在res文件下有个drawable文件夹,在那里创建frameAnimation的文件。调用时:R.drawable.filename
基本标签:
animation-list 必须存在并且必须要作为根元素,可以包含一或多个item元素。它有自己的属性 android:oneshot 如果定义为true的话,此动画只会执行一次,如果为false则一直循环。
item 作为每一帧动画的图片资源。它有自己的属性,android:drawable,设置图片drawable资源。android:duration 整数型,毫秒值。设置每帧播放的动画。
那么看看实例:
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/img1" android:duration="500"></item>
<item android:drawable="@drawable/img2" android:duration="500"></item>
<item android:drawable="@drawable/img1" android:duration="500"></item>
<item android:drawable="@drawable/img2" android:duration="500"></item>
</animation-list>
在Activity中具体使用方式。
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();
如果想要使用纯java代码实现,而不使用xml设置。
AnimationDrawable drawable = new AnimationDrawable();
for (int i = 1; i < 3; i++) {
String path = "img"+i;
int id = getResources().getIdentifier(path,"drawable",getPackageName());
Drawable drawable1 = getResources().getDrawable(id);
drawable.addFrame(drawable1,500);
}
drawable.setOneShot(false);
image.setBackgroundDrawable(drawable);
drawable.start();
这里有个小技巧,就是将字符串的命名id转换为int资源id。
int id = getResources().getIdentifier(path,"drawable",getPackageName());
好了,基础动画的使用也就这么多了。我这个学习笔记很多事参考官网的文档和启航的基础动画讲解。