湖畔镇

动画相关

本文介绍Android中动画相关内容

android.animation

Animator

  • ValueAnimator
    值动画
    • TimeAnimator
    • ObjectAnimator
      对象动画
  • AnimatorSet
    动画集合
1
2
3
4
5
public void addListener (Animator.AnimatorListener listener)
public void addPauseListener (Animator.AnimatorPauseListener listener)
public void removeAllListeners ()
public void removeListener (Animator.AnimatorListener listener)
public void removePauseListener (Animator.AnimatorPauseListener listener)

添加和删除监听器

1
2
3
4
5
public void cancel ()
public void end ()
public void pause ()
public void resume ()
public void start ()

动画进程控制

1
2
3
public boolean isPaused ()
public abstract boolean isRunning ()
public boolean isStarted ()

获得动画状态

1
public void setTarget (Object target)

设置动画的对象

1
2
public void setupEndValues ()
public void setupStartValues ()

并不清楚实际作用

属性

startDelay:开始的延迟
duration:持续时间
interpolator:插值器

ValueAnimator

1
2
3
4
public static ValueAnimator ofFloat (float... values)
public static ValueAnimator ofInt (int... values)
public static ValueAnimator ofObject (TypeEvaluator evaluator, Object... values)
public static ValueAnimator ofPropertyValuesHolder (PropertyValuesHolder... values)

通过这些获得ValueAnimator

1
2
3
public void addUpdateListener (ValueAnimator.AnimatorUpdateListener listener)
public void removeAllUpdateListeners ()
public void removeUpdateListener (ValueAnimator.AnimatorUpdateListener listener)

添加和移除监听器

1
public void reverse ()

倒序播放

属性

startDelay:开始延时
currentPlayTime:当前播放时间
duration:持续时间
evaluator:求值器
interpolator:插值器
frameDelay:帧延时
repeatCount:重复次数,默认是0,如果大于0INFINITE,那么repeatMode有效
repeatMode:重复模式,取值RESTARTREVERSE

ValueAnimator.AnimatorUpdateListener

接口

1
public abstract void onAnimationUpdate (ValueAnimator animation)

监听值更新

TimeAnimator

提供一个简单的回调机制,在动画的每一帧通知你

1
public void setTimeListener (TimeAnimator.TimeListener listener)

设置监听器

TimeAnimator.TimeListener

接口

1
public abstract void onTimeUpdate (TimeAnimator animation, long totalTime, long deltaTime)

监听帧变化

ObjectAnimation

1
2
3
4
5
6
7
public static ObjectAnimator ofFloat (T target, Property<T, Float> property, float... values)
public static ObjectAnimator ofFloat (Object target, String propertyName, float... values)
public static ObjectAnimator ofInt (T target, Property<T, Integer> property, int... values)
public static ObjectAnimator ofInt (Object target, String propertyName, int... values)
public static ObjectAnimator ofObject (T target, Property<T, V> property, TypeEvaluator<V> evaluator, V... values)
public static ObjectAnimator ofObject (Object target, String propertyName, TypeEvaluator evaluator, Object... values)
public static ObjectAnimator ofPropertyValuesHolder (Object target, PropertyValuesHolder... values)

通过这些获得ObjectAnimation

1
public void setAutoCancel (boolean cancel)

当运行其他ObjectAnimator时是否自动取消

1
public ObjectAnimator setDuration (long duration)

设置持续时间

1
2
public void setProperty (Property property)
public void setPropertyName (String propertyName)

设置要作用的属性

AnimatorSet

属性

startDelay:开始延时
duration:持续时间
interpolator:插值器

1
public AnimatorSet.Builder play (Animator anim)

构建一个Builder对象,开始播放

1
2
public void playSequentially (Animator... items)
public void playSequentially (List<Animator> items)

顺序播放

1
2
public void playTogether (Collection<Animator> items)
public void playTogether (Animator... items)

同时播放

AnimatorSet.Builder

内部类,用来方便的增加动画

1
2
3
4
public AnimatorSet.Builder after (Animator anim)
public AnimatorSet.Builder after (long delay)
public AnimatorSet.Builder before (Animator anim)
public AnimatorSet.Builder with (Animator anim)

用这四个函数来播放控制

例1:

1
2
3
4
AnimatorSet s = new AnimatorSet();
s.play(anim1).with(anim2); // 1和2同时播放
s.play(anim2).before(anim3); // 2在3之前播放
s.play(anim4).after(anim3); // 4在3之后播放

例2:

1
2
  AnimatorSet s = new AnimatorSet();
s.play(anim1).before(anim2).before(anim3); // 1结束后同时播放2和3

所以如果要顺序播放1、2、3就要这样

1
2
3
AnimatorSet s = new AnimatorSet();
s.play(anim1).before(anim2);
s.play(anim2).before(anim3);

Animator.AnimatorListener

接口

1
2
3
4
public abstract void onAnimationCancel (Animator animation)
public abstract void onAnimationEnd (Animator animation)
public abstract void onAnimationRepeat (Animator animation)
public abstract void onAnimationStart (Animator animation)

监听开始、结束、取消和重复

Animator.AnimatorPauseListener

接口

1
2
public abstract void onAnimationPause (Animator animation)
public abstract void onAnimationResume (Animator animation)

监听暂停和继续

AnimatorListenerAdapter

提供了Animator.AnimatorListenerAnimator.AnimatorPauseListener空实现的便利类

AnimatorInflater

从XML初始化动画

1
public static Animator loadAnimator (Context context, int id)

TypeEvaluator

接口,翻译成估值器,方便为任意类型提供动画效果

1
public abstract T evaluate (float fraction, T startValue, T endValue)

fraction 理解成运行的百分比,startValue 开始值,endValue 结束值,返回算出来的结果,一般计算是简单的startValue + fraction * (endValue - startValue)

  • IntEvaluator
  • FloatEvaluator
  • RectEvaluator 矩形每条边各自计算
  • ArgbEvaluator 每个通道各自计算

KeyFrame

关键帧,为动画保存时间-值对,是一个抽象类

1
2
3
4
5
6
public static Keyframe ofFloat (float fraction, float value)
public static Keyframe ofFloat (float fraction)
public static Keyframe ofInt (float fraction)
public static Keyframe ofInt (float fraction, int value)
public static Keyframe ofObject (float fraction)
public static Keyframe ofObject (float fraction, Object value)

通过这些构造关键帧

属性

fraction:时间,百分比0-1之间
value:值
interpolator:可选的插值器

PropertyValuesHolder

保存在一个动画中对应的属性和其变化的值,可用来创建动画同时作用在几个不同属性上

1
2
3
4
5
6
7
8
public static PropertyValuesHolder ofFloat (String propertyName, float... values)
public static PropertyValuesHolder ofFloat (Property<?, Float> property, float... values)
public static PropertyValuesHolder ofInt (Property<?, Integer> property, int... values)
public static PropertyValuesHolder ofInt (String propertyName, int... values)
public static PropertyValuesHolder ofKeyframe (Property property, Keyframe... values)
public static PropertyValuesHolder ofKeyframe (String propertyName, Keyframe... values)
public static PropertyValuesHolder ofObject (Property property, TypeEvaluator<V> evaluator, V... values)
public static PropertyValuesHolder ofObject (String propertyName, TypeEvaluator evaluator, Object... values)

例子,显示了viewwidth跟随5个关键帧变化

1
2
3
4
5
6
7
8
9
Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvh = PropertyValuesHolder.ofKeyframe("width", kf0, kf1, kf2, kf4, kf3);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvh);
animator.setDuration(2000);
animator.start();

TimeInterpolator

接口,时间插值器,就是提供一个跟随时间变化的函数,描述动画的变化率

1
public abstract float getInterpolation (float input)

映射input到动画的fractioninput0-1之间线性变化,返回的值可能大于1(overshoot的情况)

  • AccelerateDecelerateInterpolator
  • AccelerateInterpolator
  • AnticipateInterpolator
  • AnticipateOvershootInterpolator
  • BounceInterpolator
  • CycleInterpolator
  • DecelerateInterpolator
  • Interpolator
  • LinearInterpolator
  • OvershootInterpolator

LayoutTransition

布局变换,调用ViewGroup.setLayoutTransition()添加布局动画,调用setAnimator()自定义动画
有两种不同的变化:添加/移除View,改变Visiblity会触发同样的逻辑
有四种因此产生的动画

1
2
public void addTransitionListener (LayoutTransition.TransitionListener listener)
public void removeTransitionListener(LayoutTransition.TransitionListener listener)

添加和移除布局变换监听器

1
2
public void disableTransitionType (int transitionType)
public void enableTransitionType (int transitionType)

允许或禁止某种变换类型

变换类型

  • APPEARING
    出现
  • CHANGE_APPEARING
    因别的东西出现而发生布局变化
  • DISAPPEARING
    消失
  • CHANGE_DISAPPEARING
    因别的东西消失而发生布局变化
  • CHANGING
    默认关闭,因不是添加移除产生的布局变化
1
2
3
public boolean isChangingLayout ()
public boolean isRunning ()
public boolean isTransitionTypeEnabled (int transitionType)

判断动画状态

1
public void setAnimator (int transitionType, Animator animator)

给某个变换类型设置动画

分享