Create layer list with rounded corners programmatically

后端 未结 1 1544
滥情空心
滥情空心 2021-01-02 05:05

I am currently trying to convert the following XML to be created programmatically so that I can set the top corners and bottom corners as needed throughout my project. It is

相关标签:
1条回答
  • 2021-01-02 05:37

    You're looking for LayerDrawable's setLayerInset in order to be able to set one rectangle above the other.

    See below:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题