I am using ShapeDrawable for LinearLayouts background. The Shapes are made by cod as I need to dynamically assign colors to them, depending on conditions. Here is my custom Shape
public class CustomShapeDrawable extends ShapeDrawable {
private final Paint fillpaint, strokepaint, linePaint = new Paint();
private int strokeWidth = 3;
private final boolean disableBottomBorder;
public CustomShapeDrawable(Shape s, int fill, int stroke, int strokewidth, boolean disablebottomborder) {
super(s);
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(strokewidth);
strokepaint.setColor(stroke);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(strokewidth + 1);
linePaint.setColor(fill);
strokeWidth = strokewidth;
disableBottomBorder = disablebottomborder;
}
public CustomShapeDrawable(Shape s, int fill, int stroke, boolean disablebottomborder) {
super(s);
fillpaint = new Paint(this.getPaint());
fillpaint.setColor(fill);
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(strokeWidth);
strokepaint.setColor(stroke);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(strokeWidth + 1);
linePaint.setColor(fill);
disableBottomBorder = disablebottomborder;
}
@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
shape.resize(canvas.getClipBounds().right, canvas.getClipBounds().bottom);
shape.draw(canvas, fillpaint);
Matrix matrix = new Matrix();
matrix.setRectToRect(new RectF(0, 0, canvas.getClipBounds().right, canvas.getClipBounds().bottom), new RectF(strokeWidth / 2, strokeWidth / 2, canvas.getClipBounds().right - strokeWidth / 2,
canvas.getClipBounds().bottom - strokeWidth / 2), Matrix.ScaleToFit.FILL);
canvas.concat(matrix);
shape.draw(canvas, strokepaint);
if (disableBottomBorder) {
canvas.drawLine(0 + strokeWidth/2, shape.getHeight(), shape.getWidth() - strokeWidth/2, shape.getHeight(), linePaint);
}
}
This CustomShapeDrawable is used as StateListDrawable for my layouts like this:
RoundRectShape shapeTopCorners = new RoundRectShape(new float[] { 10, 10, 10, 10, 0, 0, 0, 0 }, null, null);
ShapeDrawable shapeTopCornersNormal =
new CustomShapeDrawable(shapeTopCorners, Global.getFleet().getSkins().getBackgroundcolour(), context.getResources().getColor(R.color.item_line), true);
ShapeDrawable shapeTopCornersPressed =
new CustomShapeDrawable(shapeTopCorners, context.getResources().getColor(R.color.menu_grey), context.getResources().getColor(R.color.item_line), true);
StateListDrawable stateTopCornersRounded = new StateListDrawable();
stateTopCornersRounded.addState(new int[] { android.R.attr.state_focused }, shapeTopCornersPressed);
stateTopCornersRounded.addState(new int[] { android.R.attr.state_pressed }, shapeTopCornersPressed);
stateTopCornersRounded.addState(new int[] {}, shapeTopCornersNormal);
Everything looks ok, the layout has the same shape with the color I want. The ugly thing happens when on screen I get another element, like Keyboard or a AlertDialog. When my app gets focus again, the layouts go crazy with random lines and artifacts on them. Here is what I mean:
What can I do to prevent or fix this artifacts as they look ugly. I have no idea why this happens in the first place. Thank you for any help you may provide me with.
Not having any suggestions, the only thing that fixes things for now is: - I made a method in my activities, called invalidate() in which for all layouts I wanted to refresh, i added layoutId.invalidate() - whenever a alertdialog is shown, call invalidate() - for all EditTexts onFocusChanged or onTextChanged, call invalidate()
Not quite an ergonomic solution but it seems to work for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With