2013-02-26 22 views
8

Tôi hiện đang cố chuyển đổi XML sau để được tạo lập trình để tôi có thể đặt các góc trên cùng và góc dưới cùng khi cần trong suốt dự án của mình. Nó là một danh sách lớp đơn giản có hai hình chữ nhật; một trên đầu trang của người khác. Tôi muốn sử dụng điều này như một nền tảng cho một vài quan điểm khác nhau vì vậy điều quan trọng là kết quả quy mô.Tạo danh sách lớp với các góc tròn theo lập trình

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:bottom="20dp"> 
     <shape android:shape="rectangle" > 
      <size android:height="20dp" /> 
      <solid android:color="#969595" /> 
      <corners 
       android:radius = "0dp" 
       android:topLeftRadius="5dp" 
       android:topRightRadius="5dp" /> 
     </shape> 
    </item> 
    <item android:top="20dp"> 
     <shape android:shape="rectangle" > 
      <size android:height="20dp" /> 
      <solid android:color="#7B7979" /> 
      <corners 
       android:radius = "0dp" 
       android:bottomLeftRadius="5dp" 
       android:bottomRightRadius="5dp" /> 
     </shape> 
    </item> 
</layer-list> 

Phương pháp này hoạt động nhưng tôi cần một XML riêng cho mỗi hình dạng tùy thuộc vào việc tôi muốn đầu, dưới, cả hai hoặc không có góc nào được làm tròn.

nỗ lực hiện tại của tôi trong việc tạo cùng một đối tượng có thể vẽ không mang lại gì nhiều hơn hai hình chữ nhật với một hình chữ nhật trên đầu trang của hình khác. Tôi không thể tìm ra cách thiết lập vị trí của hình chữ nhật. Tôi có thể thấy không có thay đổi rõ ràng bất kể những giới hạn của hình dạng đã được đặt thành. Bất kỳ đề xuất sẽ được đánh giá rất cao.

// Usage: 
setBackgroundDrawable(new DualColorStateDrawable(0, 10f)); 

...

private final int topColorUnselected = Color.RED; 
private final int bottomColorUnselected = Color.GREEN; 
private final int topColorSelected = Color.YELLOW; 
private final int bottomColorSelected = Color.BLUE; 
private final int m_nZERO_RADIUS = 0; 

class DualColorStateDrawable extends StateListDrawable 
{ 
    public NYWTableViewCellStateDrawable(float topRadius, float bottomRadius){ 
     addState(new int[] { -android.R.attr.state_pressed }, 
       createListWithSelectedState(false, topRadius, bottomRadius)); 
     addState(new int[] { android.R.attr.state_pressed }, 
       createListWithSelectedState(true, topRadius, bottomRadius)); 
    } 

    public Drawable createListWithSelectedState(
     boolean isSelected, float topRadius, float bottomRadius){ 

     int topColor, bottomColor; 

     if (isSelected) { 
      topColor = topColorSelected; 
      bottomColor = bottomColorSelected; 
     } else { 
      topColor = topColorUnselected; 
      bottomColor = bottomColorUnselected; 
     } 

     int x = 10; 
     int y = 10; 
     int width = 20; 
     int height = 20; 

     RoundRectShape _rrsTopShape = 
      new RoundRectShape(getRadii(topRadius, m_nZERO_RADIUS), null, null); 
     CustomShapeDrawable _csdTopShape = 
      new CustomShapeDrawable(_rrsTopShape, topColor); 
     RoundRectShape _rrsBottomShape = 
      new RoundRectShape(getRadii(m_nZERO_RADIUS, bottomRadius), null, null); 
     CustomShapeDrawable _csdBottomShape = 
      new CustomShapeDrawable(_rrsBottomShape, bottomColor); 
     _csdBottomShape.setBounds(x, y, x + width, y + height); 

     return new LayerDrawable(new Drawable[] {_csdTopShape, _csdBottomShape}); 
    } 

    private float[] getRadii(float top, float bottom) 
    { 
     return new float[] { top, top, // 
       top, top, // 
       bottom, bottom, // 
       bottom, bottom // 
     }; 
    } 

    class CustomShapeDrawable extends ShapeDrawable { 
     private final Paint fillpaint; 

     public CustomShapeDrawable(Shape s, int fill) { 
      super(s); 
      fillpaint = new Paint(this.getPaint()); 
      fillpaint.setColor(fill); 
     } 

     @Override 
     protected void onDraw(Shape shape, Canvas canvas, Paint paint) { 
      shape.draw(canvas, fillpaint); 
     } 
    } 
} 

Trả lời

15

Bạn đang tìm kiếm LayerDrawable 's setLayerInset để có thể thiết lập một hình chữ nhật ở trên kia.

Xem dưới đây:

float radius = 5.0f; 

float[] m_arrfTopHalfOuterRadii = 
    new float[] {radius, radius, radius, radius, 0, 0, 0, 0}; 
float[] m_arrfBottomHalfOuterRadii = 
    new float[] {0, 0, 0, 0, radius, radius, radius, radius}; 

int m_nTopColor = Color.BLUE; 
int m_nBottomColor = Color.CYAN; 

int m_nCellHeight = 40; 

public Drawable drawbg() 
{ 
    RoundRectShape top_round_rect = 
     new RoundRectShape(m_arrfTopHalfOuterRadii, null, null); 
    ShapeDrawable top_shape_drawable = new ShapeDrawable(top_round_rect); 
    top_shape_drawable.getPaint().setColor(m_nTopColor); 

    RoundRectShape bottom_round_rect = 
     new RoundRectShape(m_arrfBottomHalfOuterRadii, null, null); 
    ShapeDrawable bottom_shape_drawable = new ShapeDrawable(bottom_round_rect); 
    bottom_shape_drawable.getPaint().setColor(m_nBottomColor); 

    Drawable[] drawarray = {top_shape_drawable, bottom_shape_drawable}; 
    LayerDrawable layerdrawable = new LayerDrawable(drawarray); 

    int _nHalfOfCellHeight = m_nCellHeight/2; 
    layerdrawable.setLayerInset(0, 0, 0, 0, _nHalfOfCellHeight); //top half 
    layerdrawable.setLayerInset(1, 0, _nHalfOfCellHeight, 0, 0); //bottom half 

    return layerdrawable; 
} 
Các vấn đề liên quan