Android官方ApiDemo中animation部分代码要点整理

来源:互联网 发布:360极速浏览器 mac版 编辑:程序博客网 时间:2024/06/06 06:48


ApiDemo动画代码技术点整理

ApiDemo中com.example.android.apis.animation包内有10多个demo源码,主要是最新的属性动画,少量补间动画,还有布局动画及最新的Transitions框架,以下是这个包中各个demo源码的要点整理。在这个包之外还有很多动画,多是补间动画及相关布局动画,代码比较分散,搜索关键字是“animation”和布局相关的“Transition”等

AnimationCloning
     clone
     AnimatorSet,也可以clone

AnimationLoading
     XML定义animator
          ValueAnimator和ObjectAnimator的XML定义不同,后者要提供属性名

AnimationSeeking
     bounceAnim.setCurrentPlayTime(seekTime);设置当前时间位置

AnimatorEvents
     event:start/cancel/repeat/end
     同一个对象,两个动画同时播放?说明动画是UI线程之外的异步线程?

BouncingBalls
            AnimatorSet bouncer = new AnimatorSet();
            bouncer.play(bounceAnim).before(squashAnim1);
            bouncer.play(squashAnim1).with(squashAnim2);
            bouncer.play(squashAnim1).with(stretchAnim1);
            bouncer.play(squashAnim1).with(stretchAnim2);
            bouncer.play(bounceBackAnim).after(stretchAnim2);

     要点是多个动画的播放次序、依赖关系
     参考
          http://developer.android.com/reference/android/animation/AnimatorSet.html
          http://developer.android.com/reference/android/animation/AnimatorSet.Builder.html

CustomEvaluator
         public class XYEvaluator implements TypeEvaluator {
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            XYHolder startXY = (XYHolder) startValue;
            XYHolder endXY = (XYHolder) endValue;
            return new XYHolder(startXY.getX() + fraction * (endXY.getX() - startXY.getX()),
                    startXY.getY() + fraction * (endXY.getY() - startXY.getY()));
        }
    }

LayoutAnimationsByDefault
     使用系统缺省动画,无需再编写代码
          android:animateLayoutChanges="true"

FixedGridLayout
     最简的自定义布局
     可以作为模板
     自定义布局的复杂度取决于要支持多少布局参数,如果很少,或如此例是Fixed的,那么自定义布局很简单

LayoutAnimations
     布局动画控制器android.animation.LayoutTransition
     增加和删除两种事件,导致4种动画
     KeyFrame
     PropertyValuesHolder及对应的构造方法ObjectAnimator.ofPropertyValuesHolder
     可变参数导致动画效果
     被影响的child很多,但只设置一个动画,其他拷贝

LayoutAnimationsHideShow
     似乎没有新东西

ListFlipper
     绕Y轴(rotationY)或X轴旋转,而不是绕Z轴(rotation)旋转
     3D到平面的投影系统
     将View看成三维空间中的一个平面

MultiPropertyAnimation
     带光影的ball的画法

ReversingAnimation
     带光影的ball的画法

Rotate3dAnimation
     public class Rotate3dAnimation extends Animation {
     覆盖applyTransformation(),在其中实现自定义投影及动画
          需要查询Camera和Matrix资料才能看明白
     Camera ---- 照相机、投影机?
          A camera instance can be used to compute 3D transformations and
          generate a matrix that can be applied, for instance, on a Canvas.
          http://developer.android.com/reference/android/graphics/Camera.html
     Matrix类
     补间动画
          http://developer.android.com/reference/android/view/animation/package-summary.html
     可能是补间动画的操作
          mContainer.startAnimation(rotation);

Transition3d
     在一个动画之后接另一个动画,通过container.post()来实现。注意post的参数是一个Runnable
         private final class DisplayNextView implements Animation.AnimationListener {
         public void onAnimationEnd (Animation animation) {
            mContainer.post(new SwapViews(mPosition));
        }
    }

Transitions
     Scene
     TransitionManager
     注,Transitions框架需要在Android 4.4以后版本中运行





0 0