android自定义view onDraw canvas

来源:互联网 发布:编程就业培训中心 编辑:程序博客网 时间:2024/05/16 12:38

android自定义view onDraw

  1. canvas ,paint方法
  2. 在onMeaure和onLayout完成后的onDraw方法

canvas

canvas 简单来说就是一块画布,当我们需要进行绘制时候,他就像一张纸,可以在上面任意的书写,作画,paint,就像他的名字一样,用来书写以及画画的工具,通过设置一系列的paint参数,再将paint传入canva中便可完成绘制。
可以看下官方api对于canvas的描述

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).

如果要画一些东西的话,需要4个基本的组成部分,一个来装载像素的位图(bitmap),一个canvas向bitmap中写入内容,一个需要绘制的形状(如方形,圆形,或者图片)和一个画笔(paint).

Canvas()Construct an empty raster canvas.Canvas(Bitmap bitmap)Construct a canvas with the specified bitmap to draw into.

上面是是它的构造方法,相信都能看懂,第一个是一个空的构造,下面是直接传入一个bitmap的构造方法。同时也可以通过setBitmap来进行设置位图。

当我们指定好需要的位图,设置好绘制的图形,和设置好paint的方式以后就可以调用canvas的方法,例如 drawLine方法实现画图的操作。

自定义view中的ondraw方法中传入了一个canvas参数,他已经指定了位图,所以可以直接进行绘图操作。
如果实际时候没有通过onDraw方法,则可以自己的设定类图的大小,通过设定bitmap来进行设定。

原创粉丝点击