View的绘制过程详解
扫描二维码
随时随地手机看文章
1、当测量好一个View后就可以重写onDraw()方法,并在Canvas对象上绘制所需的图形。
public class
Canvas
extends Object
java.lang.Object
↳
android.graphics.CanvasCanvas()Construct an empty raster canvas.
Canvas(Bitmap bitmap)Construct a canvas with the specified bitmap to draw into.
Class Overview
The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
2、onDraw
protected void onDraw (Canvas canvas)
Implement this to do your drawing.
参数Canvas canvas对象用来进行绘图。
Canvas canvas = new Canvas(bitmap);
bitmap用来存储所有绘制在Canvas上的像素信息。
由源码:View的绘制过程有以下几步:
1)绘制背景(background.draw(canvas))
2)绘制自己(omDraw())
3)绘制children(dispatchDraw)
4)绘制装饰(onDrawScrollBars(for instance))
View绘制过程的传递通过dispatchDraw来实现,遍历调用所有子元素的draw方法,draw时间一层层传递下去。
Public Constructors |
---|