invalidate()源码分析
来源:互联网 发布:网络生存游戏 编辑:程序博客网 时间:2024/06/12 04:12
事实上,远远没有您想象的那么简单。为了写好这篇博客,还是拿例子说事吧。
- 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();
- }
- }
- }
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;@Overridepublic 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; // 弧形角度@Overrideprotected 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();
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();
- }
@Overridepublic 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!
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);
- }
- }
- }
/** * 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 { }
/** * 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();
- }
- }
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();
- }
if (!mWillDrawSoon) { scheduleTraversals();}这个方法是向Handler发送消息:
- public void scheduleTraversals() {
- if (!mTraversalScheduled) {
- mTraversalScheduled = true;
- sendEmptyMessage(DO_TRAVERSAL);
- }
- }
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();
- }
- }
public void handleMessage(Message msg) {switch (msg.what) {// 、、、case DO_TRAVERSAL:// 、、、 performTraversals();}}performTraversals()方法,调用ViewRoot的私有方法private void draw(boolean fullRedrawNeeded),在该方法中有句代码很关键:
- mView.draw(canvas);
mView.draw(canvas);其实这句代码,就是调用View的draw()方法 ,关键代码:
- if (!dirtyOpaque) onDraw(canvas);
if (!dirtyOpaque) onDraw(canvas);也就是说,满足这个方法,就会回调onDraw()方法。到此为止,您应该明白,当我们自己调用invalidate()方法时,想使onDraw()方法回调,必须满足条件。
调用关系,请看草图!
- Android invalidate()源码分析
- View编程:invalidate()源码分析
- View编程(3): invalidate()源码分析
- Android requestLayout和invalidate源码分析
- invalidate()源码分析
- 天天记录 - Android 使用dmeo和源码分析invalidate流程
- Android ViewManger解析 从ViewRoot 源码分析invalidate
- Android View刷新原理Invalidate()和PostInvalidate()源码分析
- android-invalidate()源码
- Android View 绘制流程 与invalidate 和postInvalidate 分析--从源码角度
- View的两种更新方法-从源码角度分析invalidate()和postInvalidate()的区别
- Android View系统源码分析(十二)—— View.invalidate()
- invalidate()等相关方法分析
- Invalidate
- Invalidate()
- Invalidate()
- Invalidate
- Invalidate
- matlab错误
- 不需要Real-Time的四个理由
- 3DSMaxSDK 导出顶点tangent、binormal、normal的方法
- Windows完成端口与Linux epoll技术简介
- 谈EXPORT_SYMBOL使用
- invalidate()源码分析
- 实现当鼠标在按钮上时,出现tip提示信息!
- C# this关键字引用类的当前实例。
- 单身快乐(致一个人的情人节)
- 存储系统
- VC6在64位Windows7(家庭版或其他版本)下调试的时候,再结束调试,程序无法退出
- 把握本质规律——《数学之美》作者吴军
- android 代码优化
- WPF接口实例