如何为特定矩形使用不同的填充颜色

How to use different fill colour for specific rectangles

根据我用于我的 canvas 绘图的代码和相关的屏幕截图,我试图用不同的颜色填充特定的矩形,但这种意外行为发生在其他矩形发生变化的地方笔色也。有谁知道如何解决这个问题?

I only want the 2nd rectangle from the the left on the top row to be filled black & stroked red whilst the other rectangles remain with a red stroke

public class Car extends View {
    public Car(Context context) {
        super(context);
        init();
    }

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

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

    Paint paint;

    private void init() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int w = canvas.getWidth();
        int h = canvas.getHeight();

        int rectWidth = w / 5;
        int space = w / 15;
        int topRectHeight = getPaddingTop();
        int bottomRectHeight = getPaddingBottom();

        paint.setStyle(Paint.Style.STROKE); //add this
        for (int i = 0; i < 4; i++) {
            int left = i * (rectWidth + space);
            int right = left + rectWidth;

            if (i == 1){
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.BLACK);
            }
            else{
                paint.setColor(Color.RED);
            }

            Rect rect = new Rect(left, 0, right, topRectHeight);
            canvas.drawRect(rect, paint);
            paint.setStyle(Paint.Style.STROKE);//add this
            Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
            canvas.drawRect(rect2, paint);
        }
    }
}

I only want the 2nd rectangle from the the left on the top row to be filled black & stroked red whilst the other rectangles remain with a red stroke

为此,在 if 情况下 i=1 你需要做两件事

  1. setStylefillsetColorblack 并绘制矩形。
  2. 然后再次将 setStyle 设置为 stroke 并将 setColor 设置为 red 并绘制你的 rect2 .

代码:

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int w = canvas.getWidth();
        int h = canvas.getHeight();

        int rectWidth = w / 5;
        int space = w / 15;
        int topRectHeight = getPaddingTop();
        int bottomRectHeight = getPaddingBottom();

        paint.setStyle(Paint.Style.STROKE); //add this
        for (int i = 0; i < 4; i++) {
            int left = i * (rectWidth + space);
            int right = left + rectWidth;

            if (i == 1){ 
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.BLACK);
                Rect rect = new Rect(left, 0, right, topRectHeight);
                canvas.drawRect(rect, paint);
                //again set back the style here
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.RED);
                Rect rect2 = new Rect(left, 0, right, topRectHeight);
                canvas.drawRect(rect2, paint);
                //this will draw the lower rectangle! Using extra variable rect3 just for safer side.
                Rect rect3 = new Rect(left, h - bottomRectHeight, right, h);
                canvas.drawRect(rect3, paint);
            }else{
                Rect rect = new Rect(left, 0, right, topRectHeight);
                canvas.drawRect(rect, paint);
                paint.setStyle(Paint.Style.STROKE);//add this
                Rect rect2 = new Rect(left, h - bottomRectHeight, right, h);
                canvas.drawRect(rect2, paint);
            }
        }
    }