2013-02-26 136 views
8

मैं वर्तमान में निम्नलिखित XML को प्रोग्रामेटिक रूप से परिवर्तित करने की कोशिश कर रहा हूं ताकि मैं अपने पूरे प्रोजेक्ट में आवश्यक शीर्ष कोनों और नीचे कोनों को सेट कर सकूं। यह एक साधारण परत सूची है जिसमें दो आयताकार हैं; एक दूसरे के शीर्ष पर। मैं इसे कुछ अलग-अलग विचारों के लिए पृष्ठभूमि के रूप में उपयोग करना चाहता हूं, इसलिए यह महत्वपूर्ण है कि परिणाम स्केल।गोलाकार कोनों के साथ परत सूची बनाएं प्रोग्रामेटिक

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

यह दृष्टिकोण काम करता है, लेकिन मैं पर कि क्या मैं ऊपर, नीचे, दोनों, या कोनों में से कोई भी गोल चाहते आधार पर प्रत्येक आकार के लिए एक अलग एक्सएमएल की जरूरत है।

उसी ड्रॉइंग बनाने के मेरे वर्तमान प्रयासों ने दूसरे आय के शीर्ष पर दो आयताकारों से अधिक कुछ नहीं कमाया है। मैं आयताकारों की स्थिति को निर्धारित करने के बारे में नहीं समझ सका। आकार के सीमाओं को निर्धारित करने के बावजूद मैं कोई दृश्य परिवर्तन नहीं देख सकता था। किसी भी सुझाव के लिए बहुत आभार होगा।

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

उत्तर

15

आप LayerDrawable के setLayerInset कर सकें, ताकि अन्य के ऊपर एक आयत सेट करने में सक्षम होने के लिए।

नीचे देखें:

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