参考:http://www.jianshu.com/p/3683a69c38ea
1、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) */
2、ViewGroup.dispatchDraw(Canvas);如果控件为ViewGroup或者其子类,需要绘制子类
3、ViewGroup.drawChild(Canvas canvas, View child, long drawingtime) 4、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);// 执行动画变换之后,接着绘制视图 }
结论:1、Animation 动画中起关键作用的类是Transformation, animation负责计算动画的矩阵变换,Transformation负责将变换传递给Canvas.2、Animation 动画只是对Canvas做了矩阵变换,并没有修改其属性值,这是它和属性动画的最大区别。3、Alpha值的修改也在draw方法中保存了图层,不影响属性值
时间: 2024-10-24 11:08:32