Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式创建圆角图层列表_Android_Xml_Drawing_Shape_Layer List - Fatal编程技术网

Android 以编程方式创建圆角图层列表

Android 以编程方式创建圆角图层列表,android,xml,drawing,shape,layer-list,Android,Xml,Drawing,Shape,Layer List,我目前正在尝试将以下XML转换为以编程方式创建的XML,以便在整个项目中根据需要设置顶部角点和底部角点。它是一个简单的图层列表,有两个矩形;一个接一个。我想将此作为一些不同视图的背景,因此结果的缩放非常重要 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:bottom="20dp"> <shape android:sh

我目前正在尝试将以下XML转换为以编程方式创建的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>

private final int topColorUnselected=Color.RED;
private final int bottomColorUnselected=Color.GREEN;
private final int topColorSelected=Color.YELLOW;
private final int bottomColorSelected=Color.BLUE;
私人最终int m_nZERO_半径=0;
类DualColorStateDrawable扩展了StateListDrawable
{
public NYWTableViewCellStateDrawable(浮动顶半径、浮动底半径){
addState(新int[]{-android.R.attr.state_pressed},
createListWithSelectedState(false、topRadius、bottomRadius));
addState(新int[]{android.R.attr.state_pressed},
createListWithSelectedState(true、topRadius、bottomRadius));
}
公共可绘制createListWithSelectedState(
布尔值(已选择,浮点顶部半径,浮点底部半径){
int topColor,BOTTOCLOR;
如果(当选){
topColor=已选择topColor;
bottomColor=bottomColorSelected;
}否则{
topColor=未选择topColor;
bottomColor=未选择的bottomColor;
}
int x=10;
int y=10;
整数宽度=20;
整数高度=20;
RoundRectShape\u rrsTopShape=
新的RoundRectShape(getRadii(topRadius,m_nZERO_RADIUS),null,null);
自定义形状可移动_csdopshape=
新的自定义形状可刷新(_rrsTopShape,topColor);
圆形矩形形状rrsBottomShape=
新的RoundRectShape(getRadii(m_nZERO_半径,底部半径),null,null);
CustomShapedRavable _csdboottomShape=
新的定制形状可更换(_rrsBottomShape,bottomColor);
_csdBottomShape.立根(x,y,x+宽度,y+高度);
返回新的LayerDrawable(新的Drawable[]{{u csdopShape,{u csdopShape});
}
专用浮动[]getRadii(浮动顶部、浮动底部)
{
返回新的float[]{top,top//
顶,顶,//
底部,底部//
底部,底部//
};
}
类CustomShapeDrawable扩展了ShapeDrawable{
私人最终涂料;
公共自定义ShapedRavable(形状s、整型填充){
超级(s);
fillpaint=新绘制(this.getPaint());
fillpaint.setColor(填充);
}
@凌驾
受保护的空白onDraw(形状、画布、油漆){
绘制(画布、填充画);
}
}
}

您正在寻找
LayerDrawable
,以便能够将一个矩形设置在另一个矩形之上

见下文:

浮动半径=5.0f;
浮动[]m_arrfTopHalfOuterRadii=
新浮点[]{radius,radius,radius,radius,0,0,0};
浮动[]m_arrfbottomhalfoterradii=
新的浮点[]{0,0,0,0,radius,radius,radius,radius};
int m_nTopColor=Color.BLUE;
int m_nBottomColor=Color.CYAN;
int m_nCellHeight=40;
公共可支取支取
{
RoundRectShape top\u round\u rect=
新的圆形矩形(m_arrfTopHalfOuterRadii,null,null);
形状可拆卸顶部\形状可拆卸=新的形状可拆卸(顶部\圆形\矩形);
top_shape_drawable.getPaint().setColor(m_nTopColor);
RoundRect形状底部\u圆形\u rect=
新的RoundRectShape(m_arrfbottomhalfoterradii,null,null);
成型可拆卸底部\形状\可拆卸=新的成型可拆卸(底部\圆形\矩形);
底部\u形状\u可绘制的.getPaint().setColor(m\u nBottomColor);
Drawable[]drawarray={top_shape_Drawable,bottom_shape_Drawable};
LayerDrawable LayerDrawable=新的LayerDrawable(drawarray);
int\u nHalfOfCellHeight=m\u nCellHeight/2;
layerdrawable.SetLayerSet(0,0,0,0,_nHalfOfCellHeight);//上半部分
layerdrawable.SetLayerSet(1,0,n单元高度,0,0);//下半部分
返回层可绘制;
}