我爲LinearLayouts背景使用了ShapeDrawable。形狀是由鱈魚做的,因爲我需要根據條件動態地爲它們分配顏色。 這裏是我的自定義形狀Android中ShapeDrawable令人討厭的行爲
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);
}
}
這CustomShapeDrawable用作StateListDrawable我的佈局是這樣的:
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);
一切正常,該佈局具有我想要的顏色相同的形狀。醜陋的事情發生在屏幕上時,我得到另一個元素,如鍵盤或AlertDialog。當我的應用程序再次獲得焦點時,佈局會隨着它們上的隨機線條和工件而變得瘋狂。 這裏是我的意思是:
我能做些什麼來防止或因爲他們長得難看解決這個瑕疵。我不知道爲什麼這首先發生。感謝您爲我提供的任何幫助。