Annoying behavior for ShapeDrawable on Android

I am using ShapeDrawable for the LinearLayouts background. The shapes are made in a bang because I need to dynamically assign colors to them, depending on the conditions. Here is my usual form

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 such as:

  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 fine, the layout has the same shape with the color I want. An ugly thing happens when another item, such as a Keyboard or AlertDialog, appears on the screen. When my application gets focus again, the layouts go crazy with random lines and artifacts on them. Here is what I mean:

enter image description here

What can I do to prevent or correct these artifacts as they look ugly. I have no idea why this is happening in the first place. Thanks for any help you can provide me.

+6
source share
1 answer

Having no suggestions, the only thing that fixes at the moment: - I made a method in my actions called invalidate (), in which for all the layouts I wanted to update, I added layoutId.invalidate () - whenever it displays alertdialog, call invalidate () - for all EditTexts onFocusChanged or onTextChanged call invalidate ()

Not quite an ergonomic solution, but it seems to be working now.

+1
source

Source: https://habr.com/ru/post/914204/


All Articles