目录
第三步:为ViewGroup指定android:layoutAnimation属性
插值器: 插值器(接口Interpolator)决定 值 的变化模式(匀速、加速)
估值器: 估值器(接口TypeEvaluator)决定 值 的具体变化数值
View动画
View动画介绍
View动画的作用对象是View,支持4种动画效果,分别是:
平移动画 | TranslateAnimation |
缩放动画 | ScaleAnimation |
旋转动画 | TotateAnimation |
透明度动画 | AlphaAnimation |
View动画属性介绍及使用
Anim translate中 fromXDelta、toXDelta、fromYDelta、toYDelta属性
属性 | 说明 |
fromXDelta | 起始点X轴坐标,这里表示的是你要滑出来的界面View的右上角的位置,可以是数值、百分数、百分数p。右上角的位置数据可以是0,0%,0%p。如果是数值,比如100,表示在View右上角向右边加上100px,从View的右侧100px的位置开始,如果是-100,表示在View的右上角加上-100px,也就是从左侧-100px的位置开始。 |
toXDelta | 移动到终点X轴坐标,相对于View的右上角 |
fromYDelta | 从起点Y轴坐标开始,相对于VIew的右上角 |
toYDelta | 移动到终点Y轴坐标,相对于View的右上角 |
duration | 动画的持续时间 |
fillAfter | 动画结束以后View是否停留在结束位置,true表示View停留在结束位置,false则不停留 |
shareInterpolator | 表示集合中的动画是否和集合共享同一个插值器 |
使用示例
//res/anim/animation_test.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="true"
android:duration="100">
<translate android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100"/>
<rotate
android:duration="400"
android:fromDegrees="0"
android:toDegrees="90" />
</set>
如何应用上面的动画呢?如下所示
Button mButton = (Button) findViewById(R.id.button);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_test);
mButton.startAnimation(animation);
通过代码来应用动画的例子:
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(300);
mButton.startAnimation(animation);
mButton.setLayoutAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
LayoutAnimation的使用
LayoutAnimation作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子View出场时都会具有这种动画效果。这种效果常常被用在ListView上。.为了给ViewGroup的子View加上出场效果,遵循如下几个步骤:
第一步:定义LayoutAnimation
//res/anim/anim_layuout.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/animation_item">
</layoutAnimation>
它的属性的含义如下:
android:delay --表示子元素开始动画的时间延迟,比如子元素入场动画的时间周期是300ms,那么0.5表示每个子元素都需要延迟150ms才能播放入场动画。总体来说第一个子元素延迟150ms开始播放入场动画,第二个子元素延迟300ms开始播放入场动画,以此类推。
android:animationOrder --表示子元素动画的顺序。有三个选项:nornal(顺序)、reverse(逆序)和rondom(随机)。
第二步:为子元素指定具体的入场动画
//res/anim/anim_Item.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="true"
android:duration="100">
<translate android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100"/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
第三步:为ViewGroup指定android:layoutAnimation属性
方式一:XML中指定属性:android:layoutAnimation="@anim/anim_layout"
方式二:通过LayoutAnimationController来实现,具体代码如下:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_item);
LayoutAnimationController animationController = new LayoutAnimationController(animation);
animationController.setDelay(0.5f);
animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
recyclerView.setLayoutAnimation(animationControlle
Activity的切换效果
Activity有默认的切换效果,但是这个效果是可以自定义的,主要用到overridePendingTransition(int enterAnim, int exitAnim)这个方法,这个方法必须在StartActivity(Intent)或者finish() 之后被调用才能生效。
当启动一个Activity时,可以按照如下方式为其添加自定义的切换效果。
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
当Activity退出时,也可以为其指定自己的切换效果。
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
}
属性动画
基础认知
属性动画是API 11 Android3.0新加入的新特性,属性动画可以对任意对象的属性进行动画而不仅仅是View,动画默认时间间隔300ms,默认帧率10ms/帧。
原理:在一定时间间隔内,通过不断对值进行改变、不断将该值赋给对象的属性。从而实现该对象在该属性上的动画效果。
比较常见的动画类:ValueAnimator、ObjectAnimator和AnimatorSet,其中ObjectAnimator继承自ValueAnimator,AnimatorSet是动画集合,可以定义一组动画。
ValueAnimator类中有3个重要方法:
- ValueAnimator.ofInt(int values) 即估值器是整型估值器 - IntEvaluator
- ValueAnimator.ofFloat(float values) 浮点型估值器 - FloatEvaluator
- ValueAnimator.ofObject(int values) 无默认估值器需要自己定义实现TypeEvaluator
ValueAnimator类 & ObjectAnimator 类的区别
对比ValueAnimator类 & ObjectAnimator 类,其实二者都属于属性动画,本质上都是一致的:先改变值,然后 赋值 给对象的属性从而实现动画效果。
二者的区别在于:
ValueAnimator 类是先改变值,然后手动赋值 给对象的属性从而实现动画;是 间接 对对象属性进行操作;
ValueAnimator 类本质上是一种 改变值 的操作机制;
ObjectAnimator类是先改变值,然后自动赋值给对象的属性从而实现动画,是直接对对象属性进行操作;
可以理解为:ObjectAnimator更加智能、自动化程度更高。
属性动画使用举例
代码实现示例
示例一:改变一个对象(myObject)的translationY属性,让其沿着Y轴向上平移一段距离(它的高度),该动画在默认时间内完成
ObjectAnimator.ofFloat(myObject, "translationY", -myObject.getHeight()).start();
示例二:改变一个对象的背景色属性,典型的情形是改变View的背景色,下面的动画可以让背景色在3s内实现从0xFFFF8080到0xFF8080FF的渐变,动画会无限循环而且会有反转效果。
ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", 0xFFFF8080, 0xFF8080FF);
colorAnim.setDuration(300);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
示例三:动画集合,5 秒内对 View 的旋转、平移、缩放和透明度都进行了改变。
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(myView, "rotationx", 0, 360),
ObjectAnimator.ofFloat(myView, "rotationy", 0, 180),
ObjectAnimator.ofFloat(myview, "translationx", 0, 90),
ObjectAnimator.ofFloat(myView, "translationy", 0, 90),
ObjectAnimator.ofFloat(myView, "rotation", 0, -90),
ObjectAnimator.ofFloat(myView, "scalex", 1, 1.5f),
ObjectAnimator.ofFloat(myView, "scaley", 1, 0.5f),
ObjectAnimator.ofFloat(myView, "alpha", 1, 0.25f, 1);
set.setDuration(5 * 1000).start();
XML定义实现示例
属性动画除了通过代码实现以外,还可以通过XML来定义。属性动画需要定义在res/animator/目录下,它的语法如下所示。
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering=["together”[ "sequentially"]>
<objectAnimator android:propertyName="string"
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float ! intll color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat""reverse"]
android:valueType=["intType”l "floatType"]/>
<animator
android:duration="int"
android:repeatCount="int"
android:startOffset="int"
android:valueFrom="float int color"
android:valueTo="float int color"
android:repeatMode=["repeat" [ "reverse"]
android:valueType=["intType"l"floatType"]/>
</set>
属性动画的各种参数都比较好理解,在 XML 中可以定义 ValueAnimator、ObjectAnimator 以及 AnimatorSet,其中<set>标签对应 AnimatorSet,<animato>标签对应ValueAnimator,<obiectAnimator>而则对应 ObjectAnimator
标签属性介绍
android:ordering ----属性有两个可选值:“together”和“sequentially”,其中“together”表示动画集合中的子动画同时播放,“sequentially”则表示动画集合中的子动画按照前后顺序依次播放,属性的默认值是“together”。
android:propertyName ----表示属性动画的作用对象的属性的名称;
android:duration ----表示动画的时长;
android:valueFrom ----表示属性的起始值:
android:valueTo ----表示属性的结束值;
android:startOftset ----表示动画的延迟时间,当动画开始后,需要延迟多少毫秒才会真正播放此动画;
android:repeatCount ----表示动画的重复次数,默认值为0,其中-1表示无限循环;
android:repeatMode ----表示动画的重复模式,"repeat"连续重复,"reverse"逆向重复(第一次播放完以后,第二次会到这播放动画。如此反复)
android:valueType ---表示 android;propertyName 所指定的属性的类型,有“intType和“floatType”两个可选项,分别表示属性的类型为整型和浮点型。另外,如果android:propertyName 所指定的属性表示的是颜色,那么不需要 指定android:valueType,系统会自动对颜色类型的属性做处理。
通过XML定义一个属性动画并将其作用在View上
// res/animator/property_animator.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="300"
android:propertyName="x"
android:valueTo="200"
android:valueType="intType" />
<objectAnimator
android:duration="300"
android:propertyName="y"
android:valueTo="200"
android:valueType="intType" />
</set>
如何使用上面的属性动画呢?
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animator_test);
animatorSet.setTarget(button);
animatorSet.start();
实际开发中建议采用代码来实现属性动画,因为通过代码来实现比较简单。更重要的是,很多时候一个属性的起始值是无法提前确定的。
插值器: 插值器(接口Interpolator)决定 值 的变化模式(匀速、加速)
TimeInterpolator中文翻译为时间插值器,它的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比,系统预置的有LinearInterpolator(线性插值器:匀速动画)、AccelerateDecelerateInterpolator(加速减速插值器:动画两头慢中间快)和Decelerate-Interpolator(减速插值器:动画越来越慢)等。
Android内置了9中插值器:
估值器: 估值器(接口TypeEvaluator)决定 值 的具体变化数值
TypeEvaluator的中文翻译为类型估值算法,也叫估值器,它的作用是根据当前属性改变的百分比来计算改变后的属性值,系统预置的有IntEvaluator(针对整型属性)、FloatEvaluator(针对浮点型属性)和 ArgbEvaluator(针对Color属性)。属性动画中的插值器(Interpolator)和估值器(TypeEvaluator)很重要,它们是实现非匀速动画的重要手段。
组合动画AnimatorSet的使用
示例一:
animSet = new AnimatorSet();
animSet.play(valueAnimator).with(animColor).with(animScale);
animSet.setDuration(5000);
animSet.setInterpolator(interpolatorType);
animSet.start();
示例二:
//ordering 有两个选项:sequentially 和 together 。其中 together 为默认项。
//sequentially 表示 set 中的动画,按照先后顺序逐步进行(a 完成之后进行 b )。
//together 表示set 中的动画,在同一时间同时进行。
<set android:ordering="sequentially">
<set>
<objectAnimator
android:propertyName="x"
android:duration="500"
android:valueTo="400"
android:valueType="intType"/>
<objectAnimator
android:propertyName="y"
android:duration="500"
android:valueTo="300"
android:valueType="intType"/>
</set>
<objectAnimator
android:propertyName="alpha"
android:duration="500"
android:valueTo="1f"/>
</set>
代码引用
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myObject);
set.start();
帧动画
原理:就是将一张张单独的图片连贯的进行播放
帧动画是顺序播放一组预先定义好的图片,类似于电影播放。不同于 View动画,系统提供了另外一个类AnimationDrawable来使用帧动画。帧动画的使用比较简单,首先需要通过XML来定义一个AnimationDrawable,如下所示。
// res/drawable/frame_animation.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/image1" android:duration="500"/>
<item android:drawable="@drawable/image2" android:duration="500"/>
<item android:drawable="@drawable/image3" android:duration="500"/>
</animation-list>
然后将上述的 Drawable作为 View的背景并通过 Drawable来播放动画即可:
Button mButton = (Button)findviewById (R.id.button1);
mButton.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable drawable = (AnimationDrawable) mButton.getBackground();
drawable.start();
帧动画的使用比较简单,但是比较容易引起OOM,所以在使用帧动画时应尽量避免使用过多尺寸较大的图片。
使用动画注意事项
1.OOM问题
这个问题主要出现在帧动画中,当图片数量较多且图片较大时就极易出现OOM,这个在实际的开发中要尤其注意,尽量避免使用帧动画。
2.内存泄露
在属性动画中有一类无限循环的动画,这类动画需要在Activity退出时及时停止,否则将导致Activity无法释放从而造成内存泄露,通过验证后发现View动画并不存在此问题。
3.兼容性问题
动画在3.0 以下的系统上有兼容性问题,在某些特殊场景可能无法正常工作,因此要做好适配工作。
4. View动画的问题
View动画是对View的影像做动画,并不是真正地改变View 的状态,因此有时候会出现动画完成后View无法隐藏的现象,即 setVisibility(View.GONE)失效了,这个时候只要调用view.clearAnimation(清除View动画即可解决此问题。
5.不要使用px
在进行动画的过程中,要尽量使用dp,使用px会导致在不同的设备上有不同的效果。6.动画元素的交互
将view移动(平移〉后,在 Android 3.0以前的系统上,不管是View动画还是属性动画,新位置均无法触发单击事件,同时,老位置仍然可以触发单击事件。尽管View已经在视觉上不存在了,将View移回原位置以后,原位置的单击事件继续生效。从3.0开始,属性动画的单击事件触发位置为移动后的位置,但是 View动画仍然在原位置。
7.硬件加速
使用动画的过程中,建议开启硬件加速,这样会提高动画的流畅性。