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

时间:2015-08-28 17:10:54

标签: java android android-canvas android-view

根据我用于画布绘图的代码和关于它的屏幕截图,我试图用不同的颜色填充特定的矩形,但是这种意外的行为发生在其他矩形改变笔触颜色的地方也。有谁知道可以做些什么来解决这个问题?

  

我只希望从顶行左侧的第二个矩形填充为黑色&抚摸红色,而其他矩形保持红色划线

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);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

  

我只想要从顶行左侧开始的第二个矩形   填充黑色&amp;划动红色,而其他矩形保持与   红色中风

对于if案例i=1,你需要做两件事

  1. setStyle填充setColor黑色并绘制矩形。
  2. 然后再次将setStyle设置为描边,将setColor设置为红色并绘制你的rect2。
  3. 代码:

        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);
                }
            }
        }