绘制多个矩形

时间:2016-09-05 09:19:07

标签: android android-canvas

你能给我一些如何绘制矩形列表的建议吗?

我有一个Rectangle类:

firewall-cmd --reload

和活动

上的Rectangle实例
public final class Rectangle  extends View {

    private Rect rectangle;
    private Paint paint;

    public  Rectangle(Context context) {
        super(context);
        int x = 50;
        int y = 50;
        int sideLength = 200;

        // create a rectangle that we'll draw later
        rectangle = new Rect(x, y, sideLength, sideLength);

        // create the Paint and set its color
        paint = new Paint();
        paint.setColor(Color.GRAY);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //canvas.drawColor(Color.BLUE);
        canvas.drawRect(rectangle, paint);
    }

}

1 个答案:

答案 0 :(得分:0)

这里有一些示例三个并排的矩形

public class Rectangles extends View {

    private Paint paintBlack;

    public Rectangles(Context context) {
        super(context);
        init();
    }

    public Rectangles(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Rectangles(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paintBlack = new Paint();
        paintBlack.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawRect(0, 0, 100, 100, paintBlack);
        canvas.drawRect(105, 0, 205, 100, paintBlack);
        canvas.drawRect(210, 0, 310, 100, paintBlack);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(500, 500);
    }
}