Create layer list with rounded corners programmatically

笑着哭i 提交于 2019-11-30 12:43:47
waynesford

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!