Canvas的意思是画布,表现在屏幕上就是一块区域,我们可以再上面使用各种API绘制我们想要的东西。 一个Canvas对象有四大基本要素:
Canvas对象的获取方式有三种:
Bitmap b = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
其构造函数:
Canvas()
Construct an empty raster canvas.
Canvas(Bitmap bitmap)
Construct a canvas with the specified bitmap to draw into.
Canvas(GL gl)
Construct a canvas with the specified gl context.
Canvas类提供了一系列的draw...方法,从这些方法的名字就可以知道Canvas可以绘制的对象。
1. public void drawARGB(int a, int r, int g, int b)
Fill the entire canvas' bitmap (restricted to the current clip) with the specified ARGB color, using srcover porterduff mode.
其中第一个参数代表透明度。
2. public void drawColor(int color)
3. public void drawRGB(int r, int g, int b)
4. public void drawColor(int color, PorterDuff.Mode mode)
canvas.drawArc (扇形)
canvas.drawCircle(圆)
canvas.drawOval(椭圆)
canvas.drawLine(线)
canvas.drawPoint(点)
canvas.drawRect(矩形)
canvas.drawRoundRect(圆角矩形)
canvas.drawVertices(顶点)
cnavas.drawPath(路径)
paint.setStrokeWidth(0); 空心的宽度
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体
float y = paint.getFontMetrics().bottom - paint.getFontMetrics().top; //可用来计算字体的宽度
canvas.drawText(stepNumStr,centre-stepNumStrWidth/2,centre -2*y,paint);
常用字体类型:
Typeface.DEFAULT //常规字体类型
Typeface.DEFAULT_BOLD //黑体字体类型
Typeface.MONOSPACE //等宽字体类型
Typeface.SANS_SERIF //sans serif字体类型
Typeface.BOLD //粗体
Typeface.BOLD_ITALIC //粗斜体
Typeface.ITALIC //斜体
Typeface.NORMAL //常规
canvas.drawBitmap (位图) canvas.drawPicture (图片)
Canvas还提供了一系列位置转换的方法:rorate、scale、translate、skew(扭曲)等。