使用drawPoints方法来绘制多个像素点 drawLines方法绘制多条直线,具体说明如下:
public void drawPoints(float[] pts, int offset, int count, Paint paint)
Draw a series of points. Each point is centered at the coordinate specified by pts[], and its diameter is specified by the paint's stroke width (as transformed by the canvas' CTM), with special treatment for a stroke width of 0, which always draws exactly 1 pixel (or at most 4 if antialiasing is enabled). The shape of the point is controlled by the paint's Cap type. The shape is a square, unless the cap type is Round, in which case the shape is a circle.
Parameters
pts | Array of points to draw [x0 y0 x1 y1 x2 y2 ...] |
---|---|
offset | Number of values to skip before starting to draw. |
count | The number of values to process, after skipping offset of them. Since one point uses two values, the number of "points" that are drawn is really (count >> 1). |
paint | The paint used to draw the points |
public void drawLines(float[] pts, int offset, int count, Paint paint)
Draw a series of lines. Each line is taken from 4 consecutive values in the pts array. Thus to draw 1 line, the array must contain at least 4 values. This is logically the same as drawing the array as follows: drawLine(pts[0], pts[1], pts[2], pts[3]) followed by drawLine(pts[4], pts[5], pts[6], pts[7]) and so on.
Parameters
pts | Array of points to draw [x0 y0 x1 y1 x2 y2 ...] |
---|---|
offset | Number of values in the array to skip before drawing. |
count | The number of values in the array to process, after skipping "offset" of them. Since each line uses 4 values, the number of "lines" that are drawn is really (count >> 2). |
paint | The paint used to draw the points |
代码:
- package com.app;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.View;
- public class DrawPointsView extends View{
- public DrawPointsView(Context context) {
- super(context);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Paint paint = new Paint();
- paint.setColor(Color.RED);
- canvas.drawPoints(new float[]{10,10,30,30,50,50}, 0, 6, paint);
- canvas.drawPoints(new float[]{10,100,30,150,50,200}, 2, 4, paint);
- canvas.drawLines(new float[]{10,230,50,260,80,300,130,350}, 0, 8, paint);
- }
- }
效果图: