A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 唐杨老师 中级黑马   /  2015-3-3 12:03  /  19924 人查看  /  6 人回复  /   5 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 唐杨老师 于 2015-3-3 13:24 编辑

Android属性动画

什么是Android属性动画
    属性动画(Property Animation)系统是一个健壮的动画框架系统,它可以满足你大部分动画需求。不管动画对象是否已经绘制到屏幕上,你都可以在动画执行过程中改变它任意的属性值。一个属性动画会在一段特定长度的时间内改变一个属性(一个对象中的字段)的值。你可以通过以下几步定义一个动画:指定你要执行动画的属性,比如动画对象(View)在屏幕上的位置,指定执行时长,指定你希望的属性的变化值。

同类技术对比:
  • 补间动画(Tween Animation)
a. 渐变动画支持四种类型:平移(Translate)、旋转(Rotate)、缩放(Scale)、不透明度(Alpha)。
b. 只是显示的位置变动,View的实际位置未改变,表现为View移动到其他地方,点击事件仍在原处才能响应。
c. 组合使用步骤较复杂。
d. View Animation 也是指此动画。
  • 帧动画(Frame Animation)
a. 用于生成连续的Gif效果图。
b. DrawableAnimation也是指此动画。
  • 属性动画(Property Animation)
a. 支持对所有View能更新的属性的动画(需要属性的setXxx()和getXxx())。
b. 更改的是View实际的属性,所以不会影响其在动画执行后所在位置的正常使用。
c. Android3.0 (API11)及以后出现的功能,3.0之前的版本可使用github第三方开源库nineoldandroids.jar进行支持。

属性动画组成部分、相关类介绍:
1.ObjectAnimator :对象动画执行类。
2.ValueAnimator :值动画执行类,常配合AnimatorUpdateListener使用。
3.PropertyValuesHolder : 属性存储器,为两个执行类提供更新多个属性的功能。
4.Keyframe :为 PropertyValuesHolder提供多个关键帧的操作值。
5.AnimatorSet :一组动画的执行集合类:设置执行的先后顺序,时间等。
6.AnimatorUpdateListener :动画更新监听。
7.AnimatorListener :动画执行监听,在动画开始、重复、结束、取消时进行回调。
8.AnimatorInflater :加载属性动画的xml文件。
9.TypeEvaluator :类型估值,用于设置复杂的动画操作属性的值。
10. TimeInterpolator :时间插值,用于控制动画执行过程。

(以下源代码下载地址:http://pan.baidu.com/s/1mgFXOkK

1.ObjectAnimator对象动画执行类

介绍:
1. 通过静态方法ofInt、ofFloat、ofObject、ofPropertyValuesHolder 获取类对象。
2. 根据属性值类型选择静态方法,如view的setLeft(int left) 则选用ofInt方法, setY(float y)则选用ofFloat方法。
3. 同ValueAnimator一样,可以进行串联式使用,示例如下。
示例:
  • 简单示例:View的横向移动
  1.         // 通过静态方法构建一个ObjectAnimator对象
  2.         // 设置作用对象、属性名称、数值集合
  3.         ObjectAnimator.ofFloat(view, "translationX", 0.0F, 200.0F)
  4.         // 设置执行时间(1000ms)
  5.                 .setDuration(1000)
  6.                 // 开始动画
  7.                 .start();
复制代码


  • 复合示例:View弹性落下然后弹起,执行一次。
  1.   // 修改view的y属性, 从当前位置移动到300.0f
  2.         ObjectAnimator yBouncer = ObjectAnimator.ofFloat(view, "y",
  3.                 view.getY(), 300.0f);
  4.         yBouncer.setDuration(1500);
  5.         // 设置插值器(用于调节动画执行过程的速度)
  6.         yBouncer.setInterpolator(new BounceInterpolator());
  7.         // 设置重复次数(缺省为0,表示不重复执行)
  8.         yBouncer.setRepeatCount(1);
  9.         // 设置重复模式(RESTART或REVERSE),重复次数大于0或INFINITE生效
  10.         yBouncer.setRepeatMode(ValueAnimator.REVERSE);
  11.         // 设置动画开始的延时时间(200ms)
  12.         yBouncer.setStartDelay(200);
  13.         // 开始动画
  14.         yBouncer.start();
复制代码

2.ValueAnimator 值动画执行类

介绍:
1. 构造方法与ObjectAnimator类似。
2. 与ObjectAnimator的区别在于ValueAnimator构造函数的参数中不包含动画“属性”信息。
3. 优点:结合动画更新监听onAnimationUpdate使用,可以在回调中不断更新View的多个属性,使用起来更加灵活。
示例:
  • View向右下角移动:
  1.     // 通过静态方法构建一个ValueAnimator对象
  2.         // 设置数值集合
  3.         ValueAnimator animator = ValueAnimator.ofFloat(0f, 200.0f);
  4.         // 设置作用对象
  5.         animator.setTarget(view);
  6.         // 设置执行时间(1000ms)
  7.         animator.setDuration(1000);
  8.         // 添加动画更新监听
  9.         animator.addUpdateListener(new AnimatorUpdateListener() {
  10.             @Override
  11.             public void onAnimationUpdate(ValueAnimator animation) {
  12.                 // 获取当前值
  13.                 Float mValue = (Float) animation.getAnimatedValue();
  14.                 // 设置横向偏移量
  15.                 view.setTranslationX(mValue);
  16.                 // 设置纵向偏移量
  17.                 view.setTranslationY(mValue);
  18.             }
  19.         });
  20.         // 开始动画
  21.         animator.start();
复制代码

3.PropertyValuesHolder 属性存储器

介绍:
为ValueAnimator提供多个操作属性及相应的执行参数。      
示例:
  • 同时修改View多个属性的动画:
  1. // 获取view左边位置
  2.         int left = view.getLeft();
  3.         // 获取view右边位置
  4.         int right = view.getRight();
  5.         // 将view左边增加10像素
  6.         PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", left,
  7.                 left + 10);
  8.         // 将view右边减少10像素
  9.         PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right",
  10.                 right, right - 10);
  11.         // 在X轴缩放从原始比例1f,缩小到最小0f,再放大到原始比例1f
  12.         PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX",
  13.                 1f, 0f, 1f);
  14.         // 在Y轴缩放从原始比例1f,缩小到最小0f,再放大到原始比例1f
  15.         PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY",
  16.                 1f, 0f, 1f);
  17.         // 将PropertyValuesHolder交付给ObjectAnimator进行构建
  18.         ObjectAnimator customAnim = ObjectAnimator.ofPropertyValuesHolder(view,
  19.                 pvhLeft, pvhRight, pvhScaleX, pvhScaleY);
  20.         // 设置执行时间(1000ms)
  21.         customAnim.setDuration(1000);
  22.         // 开始动画
  23.         customAnim.start();
复制代码


4.Keyframe 关键帧

介绍:
为 PropertyValuesHolder提供关键帧的操作值集合。
示例:
  • 以下示例表示该PropertyValuesHolder进行的旋转(rotation)动画,在执行时间在0%, 50%, 100%时,其旋转角度分别为0°, 360°, 0°。动画执行过程中自动进行补间。表现为自旋360°后再转回来。
  1.         // 设置在动画开始时,旋转角度为0度
  2.         Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
  3.         // 设置在动画执行50%时,旋转角度为360度
  4.         Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
  5.         // 设置在动画结束时,旋转角度为0度
  6.         Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
  7.         // 使用PropertyValuesHolder进行属性名称和值集合的封装
  8.         PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(
  9.                 "rotation", kf0, kf1, kf2);
  10.         // 通过ObjectAnimator进行执行
  11.         ObjectAnimator.ofPropertyValuesHolder(view, pvhRotation)
  12.         // 设置执行时间(1000ms)
  13.                 .setDuration(1000)
  14.                 // 开始动画
  15.                 .start();
复制代码


5.AnimatorSet 执行集合类

介绍:
1. 为多个属性动画提供播放顺序控制(注意play,with,after,before的用法)。
2. AnimatorSet类与AnimationSet类不能搞混,AnimatorSet在3.0及以上版本中才有。3.0之前的版本可使用第三方开源库nineoldandroids.jar进行支持,功能使用完全一致。
示例:
以下示例动画的播放顺序为
1.播放 bounceAnim;
2.同时播放 squashAnim1, squashAnim2,stretchAnim1, stretchAnim2;
3.接着播放 bounceBackAnim;
4.最后播放 fadeAnim;
  1.          AnimatorSet bouncer = new AnimatorSet();
  2.          bouncer.play(bounceAnim).before(squashAnim1);
  3.          bouncer.play(squashAnim1).with(squashAnim2);
  4.          bouncer.play(squashAnim1).with(stretchAnim1);
  5.          bouncer.play(squashAnim1).with(stretchAnim2);
  6.          bouncer.play(bounceBackAnim).after(stretchAnim2);
  7.          ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f,
  8.          0f);
  9.          fadeAnim.setDuration(250);
  10.          AnimatorSet animatorSet = new AnimatorSet();
  11.          animatorSet.play(bouncer).before(fadeAnim);
  12.          animatorSet.start();
复制代码

6. AnimatorUpdateListener动画更新监听

介绍:
1.在动画执行过程中,每次更新都会调用该回调,可以在该回调中手动更新view的属性。
2.当调用的属性方法中没有进行View的重绘时,需要进行手动触发重绘。设置AnimatorUpdateListener监听,并在onAnimationUpdate回调中执行View的invalidate()方法。
示例:
  • 1. 在回调中手动更新View对应属性:
  1.         // 1. 在回调中手动更新View对应属性:
  2.         AnimatorUpdateListener l = new AnimatorUpdateListener() {
  3.             public void onAnimationUpdate(ValueAnimator animation) {
  4.                 // 当前的分度值范围为0.0f->1.0f
  5.                 // 分度值是动画执行的百分比。区别于AnimatedValue。
  6.                 float fraction = animation.getAnimatedFraction();
  7.                 // 以下的的效果为 View从完全透明到不透明,
  8.                 view.setAlpha(fraction);
  9.                 // Y方向向下移动300px的距离.
  10.                 view.setTranslationY(fraction * 300.0f);
  11.             }
  12.         };
  13.         ValueAnimator mAnim = ValueAnimator.ofFloat(0f, 1.0f);
  14.         mAnim.addUpdateListener(l);
  15.         mAnim.setDuration(1000);
  16.         mAnim.start();
复制代码


  • 2. 在自定义View内部用于引发重绘:
  1. // 2. 在自定义View内部用于引发重绘
  2.     public class MyAnimationView extends View implements
  3.             ValueAnimator.AnimatorUpdateListener {
  4.         public MyAnimationView(Context context) {
  5.             super(context);
  6.         }
  7.         @Override
  8.         public void onAnimationUpdate(ValueAnimator animation) {
  9.             // 手动触发界面重绘
  10.             invalidate();
  11.         }
  12.     }
复制代码
7.AnimatorListener 动画执行监听

介绍:
1. 实现AnimatorListener中的方法可在动画执行全程进行其他任务的回调执行。
2. 也可以添加AnimatorListener的实现类AnimatorListenerAdapter,仅重写需要的监听即可。
示例:
  1. // 将view透明度从当前的1.0f更新为0.5f,在动画结束时移除该View
  2.         ObjectAnimator anim = ObjectAnimator.ofFloat(view, "alpha", 0.5f);
  3.         anim.setDuration(1000);
  4.         anim.addListener(new AnimatorListener() {
  5.             @Override
  6.             public void onAnimationStart(Animator animation) {
  7.                 // 动画开始时调用
  8.             }

  9.             @Override
  10.             public void onAnimationRepeat(Animator animation) {
  11.                 // 动画重复时调用
  12.             }
  13.             @Override
  14.             public void onAnimationEnd(Animator animation) {
  15.                 // 动画结束时调用
  16.                 ViewGroup parent = (ViewGroup) view.getParent();
  17.                 if (parent != null)
  18.                     parent.removeView(view);
  19.             }
  20.             @Override
  21.             public void onAnimationCancel(Animator animation) {
  22.                 // 动画取消时调用
  23.             }
  24.         });
  25.         anim.start();
复制代码

8.AnimatorInflater 动画加载器

介绍:
1. 属性动画可以通过xml文件的形式加载。
2. set标签内的animator也可单独使用。
3. XML语法如下:
  1. <setandroid:ordering=["together" ¦ "sequentially"]>
  2.           <objectAnimator
  3.               android:propertyName="string"
  4.               android:duration="int"
  5.               android:valueFrom="float¦ int ¦ color"
  6.               android:valueTo="float¦ int ¦ color"
  7.               android:startOffset="int"
  8.               android:repeatCount="int"
  9.               android:repeatMode=["repeat"¦ "reverse"]
  10.               android:valueType=["intType"¦ "floatType"]/>
  11.           <animator
  12.               android:duration="int"
  13.               android:valueFrom="float¦ int ¦ color"
  14.               android:valueTo="float¦ int ¦ color"
  15.               android:startOffset="int"
  16.               android:repeatCount="int"
  17.               android:repeatMode=["repeat"¦ "reverse"]
  18.               android:valueType=["intType"¦ "floatType"]/>
  19.           <set>
  20.               ...
  21.           </set>
  22. </set>
复制代码
示例:
  1.         // 加载xml属性动画
  2.         Animator anim = AnimatorInflater
  3.                 .loadAnimator(this, R.anim.animator_set);
  4.         anim.setTarget(view);
  5.         anim.start();
复制代码
xml文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set>
  3.     <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  4.         android:duration="1000"
  5.         android:valueTo="200"
  6.         android:valueType="floatType"
  7.         android:propertyName="x"
  8.         android:repeatCount="1"
  9.         android:repeatMode="reverse"/>
  10.     <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  11.         android:duration="1000"
  12.         android:valueTo="400"
  13.         android:valueType="floatType"
  14.         android:propertyName="y"
  15.         android:repeatCount="1"
  16.         android:repeatMode="reverse"/>
  17. </set>
复制代码



9.TypeEvaluator  类型估值

介绍:
1. TypeEvaluator可传入参数值的类型(本例为PointF)。
2. 重写函数public T evaluate(floatfraction, T startValue, T endValue);实现不同需求值的计算。
3. 注意fraction的使用,fraction是从开始到结束的分度值0.0 -> 1.0。
示例:
  1.         // 类型估值 - 抛物线示例
  2.         TypeEvaluator<PointF> typeEvaluator = new TypeEvaluator<PointF>() {
  3.             @Override
  4.             public PointF evaluate(float fraction, PointF startValue,
  5.                     PointF endValue) {
  6.                 float time = fraction * 3;
  7.                 Log.e(TAG, time + "");
  8.                 // x方向200px/s ,y方向0.5 * 200 * t * t
  9.                 PointF point = new PointF();
  10.                 point.x = 120 * time;
  11.                 point.y = 0.5f * 200 * time * time;
  12.                 return point;
  13.             }
  14.         };
  15.         ValueAnimator valueAnimator = ValueAnimator.ofObject(typeEvaluator,
  16.                 new PointF(0, 0));
  17.         valueAnimator.setInterpolator(new LinearInterpolator());
  18.         valueAnimator.setDuration(3000);
  19.         valueAnimator.start();
  20.         
  21.         valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
  22.             @Override
  23.             public void onAnimationUpdate(ValueAnimator animation) {
  24.                 PointF point = (PointF) animation.getAnimatedValue();
  25.                 view.setX(point.x);
  26.                 view.setY(point.y);
  27.             }
  28.         });
复制代码



10. TimeInterpolator 时间插值器

1.    几种常见的插值器:

  
Interpolator对象
  
资源ID
功能作用
AccelerateDecelerateInterpolator
@android:anim/accelerate_decelerate_interpolator
先加速再减速
AccelerateInterpolator
@android:anim/accelerate_interpolator
加速
AnticipateInterpolator
@android:anim/anticipate_interpolator
先回退一小步然后加速前进
AnticipateOvershootInterpolator
@android:anim/anticipate_overshoot_interpolator
在上一个基础上超出终点一小步再回到终点
BounceInterpolator
@android:anim/bounce_interpolator
最后阶段弹球效果
CycleInterpolator
@android:anim/cycle_interpolator
周期运动
DecelerateInterpolator
@android:anim/decelerate_interpolator
减速
LinearInterpolator
@android:anim/linear_interpolator
匀速
OvershootInterpolator
@android:anim/overshoot_interpolator
快速到达终点并超出一小步最后回到终点

2. 自定义插值器
a.实现Interpolator(TimeInterpolator)接口;
b.重写接口函数float getInterpolation(floatinput)。

6 个回复

倒序浏览
沙发?会学习下拉刷新的功能吗?
回复 使用道具 举报
好强大,谢谢分享
回复 使用道具 举报
好强大,谢谢分享
回复 使用道具 举报
看起来好深奥啊{:2_44:}
回复 使用道具 举报
好东西,看看先
回复 使用道具 举报
杨哥,果然厉害!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马