好久没有写博客了,最近趁着放假有时间,想通过写博客沉淀一下自己,希望和大家一起分享这段时间的学习生活,不足或错误之处希望大家批评指正(马上校招就到了,这段时间刚好温习总结以前所学的知识)
附google官方View动画介绍:https://developer.android.com/guide/topics/graphics/view-animation.html
说起Android动画,我么都知道Android在3.0之前一直使用View动画,后来API 11之后推出属性动画,View动画也称为视图动画,Android为内部我们提供了Aimation框架,其中定义了透明度、旋转、缩放和位移四种常见的动画,当然除了以上四种之外还有帧动画,这里就不去介绍了,View它动画控制整个VIew,他的实现原理是:在每次绘制视图的时View所在的ViewGroup中的drawChild函数获取该View的Animation的Transformation值,然后调用canvas.concat(transformToApplay.getMatrix()),通过矩阵运算完成动画帧,如果动画完成,就继续调用invalidate()函数,启动下次绘制来驱动动画,从而完成真个动画的机制。
下面有一张表:
名称 | 标签 | 子类 | 效果 |
平移动画 | <tranlate> | TranslateAnimation | 移动View |
缩放动画 | <scale> | ScaleAnimation | 放大或缩小 |
旋转动画 | <rotate> | RotateAnimation | 旋转View |
透明度动画 | <alpha> | AlphaAnimation | 改变View的透明度 |
下面我列举一些常用的标签的含义:
android:interpolator ——————表示动画集合采用的查分器(加速、减速)
android:shareInterpolator——————表示动画是否和集合共享一个插值器,如果集合不指定茶之期,那么子动画就需要单独指定所需的插值器或者使用默认值
<translate>标签的一些属性
android:fromXDelta——————表示x的起始值
android:toXDelta———————表示x的结束值
android:fromYDelta——————表示y的起始值
android:toYDelta——————表示y的结束值
<scale>标签的一些属性
android:fromXScale——————水平方向的缩放起始值
android:toXScale——————水平方向的结束值
android:fromYScale——————竖直方向的缩放的起始值
andriod:toYscale——————竖直方向缩放的结束值
android:pivotX——————缩放的轴点的x坐标,它会影响缩放的效果
android:prvotY——————缩放的轴点的y坐标,它会影响缩放效果
<rotate>标签的一些属性
android:fromDegrees——————表示旋转开始的角度
android:toDegrees——————表示旋转结束的角度
android:pivotX——————旋转的轴点的x坐标
android:pivotY——————旋转的轴点的y坐标
<alpha>标签的一些属性
android:fromAlpha——————表示透明度的起始值
android:toAlpha——————表示透明度的结束值
android:duration——————动画持续的时间
android:fillAfter——————动画结束之后是否停留在结束的位置
接下来我们说一下View动画的两种实现方式,第一种是通过xml这种是谷歌推荐的一种方式,一般位于res/anim/xxx.xml
1、位移动画
<pre class="html" name="code">01.<?xml version="1.0" encoding="utf-8"?>
02.<set xmlns:android="http://schemas.android.com/apk/res/android">
03.<translate
04.android:repeatCount="2"
05.android:fromXDelta="0"
06.android:fromYDelta="0"
07.android:toXDelta="120"
08.android:toYDelta="120"
09.android:duration="3000"
10./>
11.<!-- fromXDelta fromYDelta 为动画起始时 X和Y坐标上的位置
12.toXDelta toYDelta为动画结束起始时 X和Y坐标上的位置
13.-->
14.</set>
调用方式:
<span style="color:#ff0000;">Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation_translate);
</span>
view.startAnimation(animation);
2、缩放动画
<pre class="html" name="code">
01.<?xml version="1.0" encoding="utf-8"?>
02.<set xmlns:android="http://schemas.android.com/apk/res/android">
03.<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
04.android:fromXScale="0.0"
05.android:fromYScale="0.0"
06.android:toXScale="5"
07.android:toYScale="5"
08.android:pivotX="50%"
09.android:pivotY="50%"
10.android:fillAfter="false"
11.android:duration="5000"
12./>
13.</set>
调用同上
3、旋转动画
01.<?xml version="1.0" encoding="utf-8"?>
02.<set xmlns:android="http://schemas.android.com/apk/res/android">
03.<rotate
04.android:interpolator="@android:anim/accelerate_interpolator"
05.android:repeatCount="2"
06.android:fromDegrees="0"
07.android:toDegrees="+360"
08.android:pivotX="50%"
09.android:pivotY="50%"
10.android:duration="3000"
11./>
调用同上
4、透明度动画
01.<?xml version="1.0" encoding="utf-8"?>
02.<set xmlns:android="http://schemas.android.com/apk/res/android">
03.<alpha
04.android:fromAlpha="0.1"
05.android:toAlpha="1.0"
06.android:duration="3000"
07./>
08.</set>
调用同上
第二种是在代码中实现。
1、位移动画
TranslateAnimation ta = new TranslateAnimation(0,200,0,300);
ta.setDuration(1000);
view.startAnimation(ta);
2、缩放动画
ScaleAnimation sa = new ScaleAnimation(0,2,0,2);
sa.setDuration(1000);
view.startAnimation(sa);
3、旋转动画
RotateAnimation ra = new RotateAnimation(0,360,100,100);
ra.setDuration(1000);
view.startAnimation(ra);
4、透明度动画
AlphaAnimation aa = new AlphaAnimation(0,1);
aa.setDuration(1000);
view.startAnimation(aa);