gpt4 book ai didi

android - Android 中 ShapeDrawable 的恼人行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:40:08 25 4
gpt4 key购买 nike

我正在为 LinearLayouts 背景使用 ShapeDrawable。形状是由 cod 制作的,因为我需要根据条件动态地为它们分配颜色。这是我的自定义形状

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)时,丑陋的事情就发生了。当我的应用程序再次获得焦点时,布局因随机线条和伪影而变得疯狂。这就是我的意思:

enter image description here

我该怎么做才能防止或修复这些看起来很丑陋的瑕疵。我不知道为什么首先会发生这种情况。感谢您为我提供的任何帮助。

最佳答案

没有任何建议,目前唯一能解决问题的是:- 我在我的 Activity 中创建了一个名为 invalidate() 的方法,其中对于我想要刷新的所有布局,我添加了 layoutId.invalidate()- 每当显示警告对话框时,调用 invalidate()- 对于所有 EditTexts onFocusChanged 或 onTextChanged,调用 invalidate()

这不是一个完全符合人体工程学的解决方案,但它现在似乎可以工作。

关于android - Android 中 ShapeDrawable 的恼人行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10330509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com