android Animation 动画绘制逻辑

 

参考:http://www.jianshu.com/p/3683a69c38ea

 

 
 
一、View.draw(Canvas) 其中步骤为:
/*
* Draw traversal performs several drawing steps which must be executed
* in the appropriate order:
*
* 1. Draw the background
* 2. If necessary, save the canvas' layers to prepare for fading
* 3. Draw view's content
* 4. Draw children
* 5. If necessary, draw the fading edges and restore layers
* 6. Draw decorations (scrollbars for instance)
*/
二、ViewGroup.dispatchDraw(Canvas);若是控件为ViewGroup或者其子类,须要绘制子类 

三、ViewGroup.drawChild(Canvas canvas, View child, long drawingtime)

四、View.draw(Canvas canvas, ViewGroup parent, long drawingTime); 这个方法里面实现动画
  // 以下代码段获取Animation的矩阵、alipa等值
  if (a != null) {
   more = applyLegacyAnimation(parent, drawingTime, a, scalingRequired);
   concatMatrix = a.willChangeTransformationMatrix();
   if (concatMatrix) {
   mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
   }
   transformToApply = parent.getChildTransformation();
  }

// 主要方法为 applyLegacyAnimation,其代码段
  final Transformation t = parent.getChildTransformation(); // 取出父控件保存的Transformation 对象
  boolean more = a.getTransformation(drawingTime, t, 1f); // 用animation中的矩阵变化值填充 对象t; 其中a为程序员设置的动画对象
  
  // Animation 的 getTransformation代码段
  final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
  applyTransformation(interpolatedTime, outTransformation); // 具体填充传入参数Transformation的方法,它是一个空方法,具体实现由子类负责

  //例如:RotateAnimation 的 applyTransformation 方法,其中代码段:
  float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime);
  float scale = getScaleFactor();

  if (mPivotX == 0.0f && mPivotY == 0.0f) {
   t.getMatrix().setRotate(degrees);
  } else {
   t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale); // 将旋转角度设置到transformation的矩阵中,其余子类也是类似逻辑
  }
   // View.draw( , , )真正实现动画的代码段:
  canvas.translate(-transX, -transY);
  canvas.concat(transformToApply.getMatrix()); // 将从动画中取出的矩阵,传递给canvas实现动画效果
  canvas.translate(transX, transY);

  // View.draw(, ,)关键代码段:
  // Fast path for layouts with no backgrounds
  if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
   mPrivateFlags &= ~PFLAG_DIRTY_MASK;
   dispatchDraw(canvas); //
  } else {
   draw(canvas);// 执行动画变换以后,接着绘制视图
  }

 

结论:一、Animation 动画中起关键做用的类是Transformation, animation负责计算动画的矩阵变换,Transformation负责将变换传递给Canvas.二、Animation 动画只是对Canvas作了矩阵变换,并无修改其属性值,这是它和属性动画的最大区别。三、Alpha值的修改也在draw方法中保存了图层,不影响属性值