Android动画分类
View动画、帧动画、属性动画(API11出现)
View动画
其作用对象是View,支持4种动画效果,分别为平移动画、缩放动画、旋转动画和透明动画。
帧动画也属于View动画,但是在动画表现形式上不太一样。
View动画的种类
对应着Animation的四个子类:TranslateAnimation(平移<translate>)、ScaleAnimation(缩放<scale>)、RotateAnimation(旋转<rotate>)、AlphaAnimation(透明度<alpha>),既可以XML来定义也可以动态代码,推荐XML。
创建动画XML文件,文件路径:res/anim/filename.xml,有固定语法。
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true" //集合动画是否共享差值器
android:duration="3000" //动画的持续时间
android:fillAfter="true"> //动画结束后View是否停在结束位置,true为是
<alpha android:fromAlpha="float" //透明度的起始值 eg:0.1 android:toAlpha="float" /> //透明度的终止值 eg:1
<scale android:fromXScale="float" //水平方向缩放的起始点 android:fromYScale="float" //竖直方向缩放的起始点 android:pivotX="float" //缩放轴点的X坐标 会影响缩放效果 android:pivotY="float" //缩放轴点的Y坐标 会影响缩放效果 android:toXScale="float" android:toYScale="float" />
<translate android:fromXDelta="float" //X的起始点 android:fromYDelta="float" android:toXDelta="float" //X的jiesudia android:toYDelta="float" />
<rotate android:pivotY="float" //旋转轴点的X坐标 android:pivotX="float" //旋转轴点的Y坐标 android:fromDegrees="float" //开始旋转的角度 0 android:toDegrees="float" /> //旋转结束的角度 180</set>
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.circle);
mButton.startAnimation(animation);
android:shareInterpolator="true" //集合动画是否共享差值器 android:duration="3000" //动画的持续时间 android:fillAfter="true"> //动画结束后View是否停在结束位置,true为是<alpha android:fromAlpha="float" //透明度的起始值 eg:0.1 android:toAlpha="float" /> //透明度的终止值 eg:1
android:pivotX
表示缩放/旋转起点 X 轴坐标,可以是整数值、百分数(或者小数)、百分数p 三种样式,比如 50、50% / 0.5、50%p。当属性值为数值时,表示在当前 View 的左上角,即原点处加上 50px,作为起始点;如果是百分数,比如 50%,表示在当前控件的左上角加上自己宽度的 50% (即自身宽度中心)作为起始点;如果是 50%p(字母 p 是 parent 的意思),取值的基数是父控件,那么 50%p 就是表示在当前的左上角加上父控件宽度的 50% 作为起始点 x 轴坐标。
帧动画
是顺序播放一组预先定义好的图片,类似于电影播放。不同于View动画,系统提供了另一个类AnimationDrawable来使用帧动画。
首先通过XML来定义一个AnimationDrawable。//res/drawable/frame_animation.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/ic_launcher_foreground"
android:duration="500" />
<item
android:drawable="@drawable/ic_launcher_foreground"
android:duration="500" />
<item
android:drawable="@drawable/ic_launcher_foreground"
android:duration="500" />
</animation-list>
circleView.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable drawable = (AnimationDrawable) circleView.getBackground();
drawable.start();
帧动画比较简单,但很容易引起OOM,所以在使用帧动画时应尽量避免使用过多尺寸较大的图片。
View动画的特殊使用场景
比如在ViewGroup中可以控制子元素的出场效果,在Activity中可以实现不同Activity之间的切换效果。
LayoutAnimation(作用于ViewGroup),为ViewGroup指定一个动画,这样当它的子元素出场时都会具有这种动画效果。eg:常常被用于ListView中,它的每个item都以一定的动画出现。
(1)首先定义LayoutAnimation,res/anim/anim_layout.xml
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/anim_item"/>
-
android:delay表示动画播放的延时,既可以是百分比,也可以是float小数。
-
android:animationOrder表示动画的播放顺序,有三个取值normal(顺序)、reverse(反序)、random(随机)。
-
android:animation指向了子控件所要播放的动画。
(2)为子元素指定具体入场动画 res/anim/anim_item.xml
android:delay表示动画播放的延时,既可以是百分比,也可以是float小数。
android:animationOrder表示动画的播放顺序,有三个取值normal(顺序)、reverse(反序)、random(随机)。
android:animation指向了子控件所要播放的动画。
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<scale
android:pivotX="0.5"
android:pivotY="0.5"
android:fromYScale="0.0"
android:toYScale="3.0"
android:fromXScale="0.0"
android:toXScale="3.0" />
<translate android:fromXDelta="500"
android:toXDelta="0"/>
</set>
(3)写入XML布局中 或者在代码中进行配置
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/anim_layout"
android:background="#fff4f7f9">
</ListView>
-
代码配置
代码配置
如果在xml中文件已经写好LayoutAnimation,可以使用AnimationUtils直接加载:
- AnimationUtils.loadLayoutAnimation(context, id)
另外还可以手动java代码编写,如:
- //通过加载XML动画设置文件来创建一个Animation对象;
- Animation animation=AnimationUtils.loadAnimation(this, R.anim.anim_item); //得到一个LayoutAnimationController对象;
- LayoutAnimationController controller = new LayoutAnimationController(animation); //设置控件显示的顺序;
- controller.setOrder(LayoutAnimationController.ORDER_REVERSE); //设置控件显示间隔时间;
- controller.setDelay(0.3); //为ListView设置LayoutAnimationController属性;
- listView.setLayoutAnimation(controller);
- listView.startLayoutAnimation();
通过代码设置可以达到同样效果。
Activity的切换效果用在Activity之间切换,方法为 overridePendingTransition(int enterAnim , int exitAnim);
这种方法必须用在startActivity(Intent)或者finish()之后有效。
属性动画
和View不同,它对作用对象进行了拓展,属性动画可以对任何对象做动画,甚至还可以没有对象。除了对作用对象进行拓展之外同时动画效果也得到了加强。属性动画中有
ValueAnimation、ObjectAnimation和
AnimationSet等概念。
其中
ObjectAnimation继承
自ValueAnimation,
AnimationSet是动画集合
。
属性动画的使用:
(1)改变一个对象(myObject) 的translationY属性,让其沿着Y轴向上平移一段距离:它的高度,该动画在默认时间内完成。
ObjectAnimator.ofFloat(myObject,"translationY",-myObject.getHeight()).start;(2)改变一个对象的背景颜色属性,典型的情形是改变View的背景色,下面动画让背景在3秒内实现渐变,动画会无限循环而且有反转的效果。
ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", 0xFFFF8080, 0XFF8080FF); colorAnim.setDuration(3000); colorAnim.setEvaluator(new ArgbEvaluator()); colorAnim.setRepeatCount(ValueAnimator.INFINITE); colorAnim.setRepeatMode(ValueAnimator.REVERSE); colorAnim.start();(3)动画集合,5秒内对View的旋转、平移、缩放和透明度都进行改变。
AnimatorSet set = new AnimatorSet(); set.playTogether( ObjectAnimator.ofFloat(myView, "rotationX", 0, 360), ObjectAnimator.ofFloat(myView, "translationX", 0, 90), ObjectAnimator.ofFloat(myView, "scaleX", 1, 1.5f), ObjectAnimator.ofFloat(myView, "alpha", 1, 0.25f, 1) ); set.setDuration(5 * 1000).start();属性动画出来代码还可以通过XML来定义。res/animator/目录下,P278
建议使用代码动态的创建属性动画。
差值器和估值器
TimeInterpolator(时间插值器):
- 作用:根据时间流逝的百分比计算出当前属性值改变的百分比。
- 系统已有的插值器:
①LinearInterpolator(线性插值器):匀速动画。
②AccelerateDecelerateInterpolator(加速减速插值器):动画两头慢,中间快。
③DecelerateInterpolator(减速插值器):动画越来越慢。
TypeEvaluator(类型估值算法,即估值器):
- 作用:根据当前属性改变的百分比来计算改变后的属性值。
- 系统已有的估值器:
①IntEvaluator:针对整型属性
②FloatEvaluator:针对浮点型属性
③ArgbEvaluator:针对Color属性