invalidate和requestLayout方法源码分析
Posted 白乾涛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了invalidate和requestLayout方法源码分析相关的知识,希望对你有一定的参考价值。
invalidate方法源码分析
在之前分析View的绘制流程中,最后都有调用一个叫invalidate的方法,这个方法是啥玩意?我们来看一下View类中invalidate系列方法的源码(ViewGroup没有重写这些方法),如下:
/**
* Mark the area defined by dirty as needing to be drawn. dirty代表需要重新绘制的脏的区域
* If the view is visible, onDraw(Canvas) 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
postInvalidate().* <b>WARNING:</b> In API 19 and below, this method may be destructive to
dirty.* @param dirty the rectangle矩形 representing表示 the bounds of the dirty region地区
*/
public void invalidate(Rect dirty) {
final int scrollX = mScrollX;
final int scrollY = mScrollY;
invalidateInternal(dirty.left - scrollX, dirty.top - scrollY,
dirty.right - scrollX, dirty.bottom - scrollY, true, false);
}
public void invalidate(int l, int t, int r, int b) {
final int scrollX = mScrollX;
final int scrollY = mScrollY;
//实质还是调用invalidateInternal方法
invalidateInternal(l - scrollX, t - scrollY, r - scrollX, b - scrollY, true, false);
}
/**
* Invalidate the whole view. 重新绘制整个View
*/
public void invalidate() {
invalidate(true);
}
public void invalidate(boolean invalidateCache) {
invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
}
//这是所有invalidate的终极调用方法
void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
boolean fullInvalidate) {......
// Propagate繁殖、传播 the damage损害的(只需要刷新的) rectangle to the parent view.
final AttachInfo ai = mAttachInfo;
final ViewParent p = mParent;
if (p != null && ai != null && l < r && t < b) {
final Rect damage = ai.mTmpInvalRect;
damage.set(l, t, r, b);
//将需要刷新的区域封装到damage中p.invalidateChild(this, damage);
//调用Parent的invalidateChild方法,传递damage给Parent}
......
}
我们看下ViewGroup的invalidateChild方法:
public final void invalidateChild(View child, final Rect dirty) {
ViewParent parent = this;
......
安卓invalidate和requestLayout还有postInvalidate的区别
invalidate和requestLayout方法源码分析
源码分析篇 - Android绘制流程requestLayout()与invalidate()流程分析
requestLayout invalidate postInvalidate