View的绘制分析

LayoutParams are used by views to tell their parents how they want to be laid out
源码:API24
ViewRootImpl类里的方法

private void performTraversals() {...}
private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {...}

private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
        int desiredWindowHeight){...}

private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
        boolean scalingRequired, Rect dirty){...}

private static int getRootMeasureSpec(int windowSize, int rootDimension){...}

View里的方法:

public final void measure(int widthMeasureSpec, int heightMeasureSpec) {...}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {...}

public void layout(int l, int t, int r, int b) {...}

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {...}

protected boolean setFrame(int left, int top, int right, int bottom) {...}

public void draw(Canvas canvas) {...}

protected void onDraw(Canvas canvas) {} //空方法体,需要子View实现不同的绘制

public void postInvalidate() {...}

public void requestLayout() {...}

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的方法

protected void measureChildWithMargins(View child,
        int parentWidthMeasureSpec, int widthUsed,
        int parentHeightMeasureSpec, int heightUsed) {}

public final void layout(int l, int t, int r, int b) {...}

protected abstract void onLayout(boolean changed,int l, int t, int r, int b);

protected void dispatchDraw(Canvas canvas) {...} //重写View的方法

ViewGroup的子类必须重写onLayout方法,通常有一个for循环,调用View的layout方法确定子View的位置。

绘制

  • What to draw, handled by Canvas
  • How to draw, handled by Paint

Paint

public MaskFilter setMaskFilter(MaskFilter maskfilter) {...}

参考:

  • http://blog.csdn.net/yanbober/article/details/46128379/

你可能感兴趣的:(View的绘制分析)