2012-04-26 28 views
6

Tôi đang sử dụng ShapeDrawable cho nền LinearLayouts. Các hình dạng được thực hiện bởi cod như tôi cần phải tự động gán màu sắc cho họ, tùy thuộc vào điều kiện. Đây là Shape tùy chỉnh của tôiHành vi gây phiền nhiễu cho một ShapeDrawable trong Android

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 này được sử dụng như StateListDrawable cho bố trí của tôi như thế này:

 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); 

Mọi thứ trông ok, cách bố trí có hình dạng tương tự với màu sắc tôi muốn. Điều xấu xí xảy ra khi trên màn hình tôi nhận được một yếu tố khác, như Bàn phím hoặc một AlertDialog. Khi ứng dụng của tôi được tập trung một lần nữa, các bố cục trở nên điên rồ với các dòng và đồ tạo tác ngẫu nhiên trên chúng. Dưới đây là những gì tôi có nghĩa là:

enter image description here

tôi có thể làm gì để ngăn ngừa hoặc sửa chữa các đồ tạo tác này khi họ trông xấu xí. Tôi không biết tại sao điều này lại xảy ra ngay từ đầu. Cảm ơn bạn vì bất kỳ sự trợ giúp nào bạn có thể cung cấp cho tôi.

Trả lời

1

Không có bất kỳ đề xuất nào, điều duy nhất khắc phục sự cố hiện tại là: - Tôi đã thực hiện một phương pháp trong hoạt động của mình, được gọi là invalidate() trong đó tất cả bố cục tôi muốn làm mới, tôi đã thêm layoutId.invalidate() - bất cứ khi nào một alertdialog được hiển thị, cuộc gọi vô hiệu() - cho tất cả EditTexts onFocusChanged hoặc OnTextChanged, cuộc gọi vô hiệu()

Không hẳn là một giải pháp gọn nhẹ nhưng có vẻ như để làm việc cho bây giờ.

Các vấn đề liên quan