View编程(3): invalidate()源码分析
2011-12-14 15:32 tang768168 阅读(431) 评论(1) 编辑 收藏 举报View编程(2): invalidate()再探博客中,说了加载View的onDraw()方法的时机以及invalidate()方法的作用。
事实上,远远没有您想象的那么简单。为了写好这篇博客,还是拿例子说事吧。
- package mark.zhang;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- public class ViewDrawTestActivity extends Activity {
- // 用于测试
- static int times = 1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MyView mView = new MyView(this);
- mView.invalidate();
- //setContentView(mView);
- }
- /**
- * 内部类,继承View
- *
- * @author mark
- */
- class MyView extends View {
- MyView(Context context) {
- super(context);
- }
- Paint vPaint = new Paint(); // 绘制样式物件
- int i = 0; // 弧形角度
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Log.d("mark", "this run onDraw() " + (times++) + " times!");
- // 设定绘图样式
- vPaint.setColor(0xff00ffff); // 画笔颜色
- vPaint.setAntiAlias(true); // 反锯齿
- vPaint.setStyle(Paint.Style.STROKE);
- // 绘制一个弧形
- canvas.drawArc(new RectF(60, 120, 260, 320), 0, i, true, vPaint);
- // 弧形角度
- if ((i += 10) > 360) {
- i = 0;
- }
- // 重绘, 再一次执行onDraw 程序
- // invalidate();
- }
- }
- }
例子没有多大的变化,只是在onCreate()方法中直接调用invalidate()方法,如:
- mView.invalidate();
这样做的目的主要是想看看,自己调用View的invalidate()方法会不会触发onDraw()方法。运行一下:
呵呵,onDraw()方法并没有执行!那么是不是因为没有调用setContentVIew()方法呢?修改onCreate()方法:
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MyView mView = new MyView(this);
- mView.invalidate();
- setContentView(mView);
- mView.invalidate();
- }
再次运行,效果:
- D/mark ( 251): this run onDraw() 1 times!
说 明,只有setContentVIew()方法中的invalidate()方法启了作用,自己调用View的invalidate()方 法,mView.invalidate()没启任何作用。但是,在MyView的onDraw()方法中调用invalidate()方法可以循环调用 onDraw()方法,类似递归。
分析一下,invalidate()方法的源码吧,在这里也许可以找到答案。
- /**
- * Invalidate the whole view. If the view is visible, {@link #onDraw} will
- * be called at some point in the future. This must be called from a
- * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
- */
- public void invalidate() {
- if (ViewDebug.TRACE_HIERARCHY) {
- ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
- }
- if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
- mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
- final ViewParent p = mParent;
- final AttachInfo ai = mAttachInfo;
- if (p != null && ai != null) {
- final Rect r = ai.mTmpInvalRect;
- r.set(0, 0, mRight - mLeft, mBottom - mTop);
- // Don't call invalidate -- we don't want to internally scroll
- // our own bounds
- p.invalidateChild(this, r);
- }
- }
- }
这里可以看到p.invalidateChild(this, r)(看源码只看关键部分,不然你会很晕!),其中p是ViewParent实例对象。ViewParent是一个接口,现在我们关心谁实现了这个接口?
通过千辛万苦的search,终于找到ViewParen的实现类ViewRoot:
- /**
- * The top of a view hierarchy, implementing the needed protocol between View
- * and the WindowManager. This is for the most part an internal implementation
- * detail of {@link WindowManagerImpl}.
- *
- * {@hide}
- */
- @SuppressWarnings({"EmptyCatchBlock"})
- public final class ViewRoot extends Handler implements ViewParent, View.AttachInfo.Callbacks { }
那么,看看该类实现的invalidateChild()方法:
- public void invalidateChild(View child, Rect dirty) {
- checkThread();
- if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
- if (mCurScrollY != 0 || mTranslator != null) {
- mTempRect.set(dirty);
- dirty = mTempRect;
- if (mCurScrollY != 0) {
- dirty.offset(0, -mCurScrollY);
- }
- if (mTranslator != null) {
- mTranslator.translateRectInAppWindowToScreen(dirty);
- }
- if (mAttachInfo.mScalingRequired) {
- dirty.inset(-1, -1);
- }
- }
- mDirty.union(dirty);
- if (!mWillDrawSoon) {
- scheduleTraversals();
- }
- }
关键代码在这儿:
- if (!mWillDrawSoon) {
- scheduleTraversals();
- }
这个方法是向Handler发送消息:
- public void scheduleTraversals() {
- if (!mTraversalScheduled) {
- mTraversalScheduled = true;
- sendEmptyMessage(DO_TRAVERSAL);
- }
- }
接下来,看看ViewRoot的Handler的handleMessage的实现:
- public void handleMessage(Message msg) {
- switch (msg.what) {
- // 、、、
- case DO_TRAVERSAL:
- // 、、、
- performTraversals();
- }
- }
performTraversals()方法,调用ViewRoot的私有方法private void draw(boolean fullRedrawNeeded),在该方法中有句代码很关键:
- mView.draw(canvas);
其实这句代码,就是调用View的draw()方法 ,关键代码:
- if (!dirtyOpaque) onDraw(canvas);
- View.draw方法执行过程:
- * 1. Draw the background
* 2. If necessary, save the canvas' layers to prepare for fading
* 3. Draw view's content --- if (!dirtyOpaque) onDraw(canvas);
* 4. Draw children --- dispatchDraw(canvas);
* 5. If necessary, draw the fading edges and restore layers
* 6. Draw decorations (scrollbars for instance) --- onDrawScrollBars(canvas);
也就是说,满足这个方法,就会回调onDraw()方法。到此为止,您应该明白,当我们自己调用invalidate()方法时,想使onDraw()方法回调,必须满足条件。
调用关系,请看草图!
转载地址:http://blog.csdn.net/androidbluetooth/article/details/6695516