Paint not fill square properly in custom view

  • Thread starter Thread starter AC Question
  • Start date Start date
A

AC Question

I have class Shape that extends View and have two setters setColor, setShapeHeight. When I get instance of Shape class, default height of shape is 150dp, color is black then I set his height to 500 (shape.setShapeHeight(500)) then change color to red (shape.setColor(Color.Red)), the problem is caused. Draw method will draw shape that will fill red color in space 150dp height but other space (from 150 do 500) will be filled in default color (black) not in red !
In every setter is put invalidate() and requestLayout() but nothing is change. I don't understand why is this happening.

In Main Activity class:
Shape shape = (Shape) findViewById(R.id.shape1);
shape.setShapeHeight(500);
shape.setColor(Color.RED);

Shape class:
@override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

setupPaint();
canvas.drawRect(0,0, shapeHeight, shapeWidth, paintShape);
}

private void setupPaint() {
paintShape = new Paint();
paintShape.setStyle(Paint.Style.FILL);
paintShape.setColor(color);
}

public void setShapeHeight(int shapeHeight) {
this.shapeHeight = shapeHeight;
invalidate();
requestLayout();
}

public void setColor(int color) {
this.color = color;
invalidate();
requestLayout();
}
 

Trending Posts

Members online

Forum statistics

Threads
956,764
Messages
6,969,932
Members
3,163,612
Latest member
Clout