在android中绘制带边框的填充矩形

时间:2023-02-09 20:36:11

Is there any way in Android to draw a filled rectangle with say a black border. My problem is that the canvas.draw() takes one paint object, and to my knowledge the paint object can't have a different color for the fill and the stroke. Is there a way around this?

在Android中是否有任何方法可以绘制带有黑色边框的填充矩形。我的问题是canvas.draw()接受一个绘制对象,据我所知,绘制对象不能有填充和描边的不同颜色。有没有解决的办法?

3 个解决方案

#1


2  

You draw a rectangle with the color of the border and the size of the rectangle plus the border, you change the color of the paint and draw again the rectangle with the normal size.

您绘制一个矩形,其边框的颜色和矩形的大小加上边框,您可以更改颜色的颜色并再次绘制具有正常大小的矩形。

#2


105  

Try paint.setStyle(Paint.Style.FILL) and paint.setStyle(Paint.Style.STROKE).

尝试使用paint.setStyle(Paint.Style.FILL)和paint.setStyle(Paint.Style.STROKE)。

Paint paint = new Paint();
Rect r = new Rect(10, 10, 200, 100);

@Override
public void onDraw(Canvas canvas) {
    // fill
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.MAGENTA); 
    canvas.drawRect(r, paint);

    // border
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.BLACK);
    canvas.drawRect(r, paint);
}

#3


9  

If you are drawing multiple views then you could also use two paints, one for the stroke and one for the fill. That way you don't have to keep resetting them.

如果要绘制多个视图,则还可以使用两个绘制,一个用于笔划,一个用于填充。这样你就不必继续重置它们了。

在android中绘制带边框的填充矩形

Paint fillPaint = new Paint();
Paint strokePaint = new Paint();

RectF r = new RectF(30, 30, 1000, 500);

void initPaints() {

    // fill
    fillPaint.setStyle(Paint.Style.FILL);
    fillPaint.setColor(Color.YELLOW);

    // stroke
    strokePaint.setStyle(Paint.Style.STROKE);
    strokePaint.setColor(Color.BLACK);
    strokePaint.setStrokeWidth(10);
}

@Override
protected void onDraw(Canvas canvas) {

    // First rectangle
    canvas.drawRect(r, fillPaint);    // fill
    canvas.drawRect(r, strokePaint);  // stroke

    canvas.translate(0, 600);

    // Second rectangle
    int cornerRadius = 50;
    canvas.drawRoundRect(r, cornerRadius, cornerRadius, fillPaint);    // fill
    canvas.drawRoundRect(r, cornerRadius, cornerRadius, strokePaint);  // stroke
}

#1


2  

You draw a rectangle with the color of the border and the size of the rectangle plus the border, you change the color of the paint and draw again the rectangle with the normal size.

您绘制一个矩形,其边框的颜色和矩形的大小加上边框,您可以更改颜色的颜色并再次绘制具有正常大小的矩形。

#2


105  

Try paint.setStyle(Paint.Style.FILL) and paint.setStyle(Paint.Style.STROKE).

尝试使用paint.setStyle(Paint.Style.FILL)和paint.setStyle(Paint.Style.STROKE)。

Paint paint = new Paint();
Rect r = new Rect(10, 10, 200, 100);

@Override
public void onDraw(Canvas canvas) {
    // fill
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.MAGENTA); 
    canvas.drawRect(r, paint);

    // border
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.BLACK);
    canvas.drawRect(r, paint);
}

#3


9  

If you are drawing multiple views then you could also use two paints, one for the stroke and one for the fill. That way you don't have to keep resetting them.

如果要绘制多个视图,则还可以使用两个绘制,一个用于笔划,一个用于填充。这样你就不必继续重置它们了。

在android中绘制带边框的填充矩形

Paint fillPaint = new Paint();
Paint strokePaint = new Paint();

RectF r = new RectF(30, 30, 1000, 500);

void initPaints() {

    // fill
    fillPaint.setStyle(Paint.Style.FILL);
    fillPaint.setColor(Color.YELLOW);

    // stroke
    strokePaint.setStyle(Paint.Style.STROKE);
    strokePaint.setColor(Color.BLACK);
    strokePaint.setStrokeWidth(10);
}

@Override
protected void onDraw(Canvas canvas) {

    // First rectangle
    canvas.drawRect(r, fillPaint);    // fill
    canvas.drawRect(r, strokePaint);  // stroke

    canvas.translate(0, 600);

    // Second rectangle
    int cornerRadius = 50;
    canvas.drawRoundRect(r, cornerRadius, cornerRadius, fillPaint);    // fill
    canvas.drawRoundRect(r, cornerRadius, cornerRadius, strokePaint);  // stroke
}