您的位置:首页 > 职场人生

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

2012-07-25 21:37 573 查看
使用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
ptsArray of points to draw [x0 y0 x1 y1 x2 y2 ...]
offsetNumber of values to skip before starting to draw.
countThe 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).
paintThe 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
ptsArray of points to draw [x0 y0 x1 y1 x2 y2 ...]
offsetNumber of values in the array to skip before drawing.
countThe 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).
paintThe 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);
}
}


效果图:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息