11.1绘制基本图形-面试例题2:绘制多个像素点和多条直线

使用drawPoints方法来绘制多个像素点 drawLines方法绘制多条直线,具体说明如下:

public void drawPoints(float[] pts, int offset, int count, Paint paint)

Since: API Level 1

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)

Since: API Level 1

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

代码:

  
  
  
  
  1. package com.app;  
  2.  
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.view.View;  
  8.  
  9. public class DrawPointsView extends View{  
  10.  
  11.     public DrawPointsView(Context context) {  
  12.         super(context);  
  13.     }  
  14.  
  15.     @Override  
  16.     protected void onDraw(Canvas canvas) {  
  17.         super.onDraw(canvas);  
  18.         Paint paint = new Paint();  
  19.         paint.setColor(Color.RED);  
  20.         canvas.drawPoints(new float[]{10,10,30,30,50,50}, 0, 6, paint);  
  21.         canvas.drawPoints(new float[]{10,100,30,150,50,200}, 2, 4, paint);  
  22.         canvas.drawLines(new float[]{10,230,50,260,80,300,130,350}, 0, 8, paint);  
  23.     }  

 

效果图:

 

 

你可能感兴趣的:(android,drawLines,drawPoints)