创建图层列表编程圆角图层、圆角、列表

2023-09-05 07:32:39 作者:獨來獨往

我目前正在以下XML转换以编程方式创建,以便根据需要在我的项目,我可以设置顶角和底角。这是一个简单的图层列表中,有两个矩形;一个在另一个顶部。我想用此作为背景几个不同的视图,因此,重要的是结果鳞。

 <层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

<项目安卓底=20dp>
    <形机器人:形状=矩形>
        <大小机器人:身高=20dp/>

        [固体机器人:颜色=#969595/>

        <角落
            机器人:半径=0dp
            机器人:topLeftRadius =5DP
            机器人:topRightRadius =5DP/>
    < /形状>
< /项目>
<项目的android:顶部=20dp>
    <形机器人:形状=矩形>
        <大小机器人:身高=20dp/>

        [固体机器人:颜色=#7B7979/>

        <角落
            机器人:半径=0dp
            机器人:bottomLeftRadius =5DP
            机器人:bottomRightRadius =5DP/>
    < /形状>
< /项目>

< /层列表>
 

本办法正常工作,但我需要一个单独的XML为每个形状取决于我是否要在顶部,底部,两者或无的边角圆润。

我目前尝试创建相同的提拉已经产生了无非两个矩形与一个在另一个的顶部上。我无法弄清楚如何设置矩形的位置。我看不出有什么明显的变化,无论什么样的形状的边界被设置为。任何建议,将不胜AP preciated。

  //使用方法:
setBackgroundDrawable(新DualColorStateDrawable(0,10F));


私人最终诠释topColorUnselected = Color.RED;
私人最终诠释bottomColorUnselected = Color.GREEN;
私人最终诠释topColorSelected = Color.YELLOW;
私人最终诠释bottomColorSelected = Color.BLUE;

私人最终诠释m_nZERO_RADIUS = 0;

类DualColorStateDrawable扩展StateListDrawable
{
    公共NYWTableViewCellStateDrawable(浮topRadius,浮bottomRadius){
        addState(新INT [] {-​​android.R.attr.state_ pressed},createListWithSelectedState(假,topRadius,bottomRadius));
        addState(新INT [] {android.R.attr.state_ pressed},createListWithSelectedState(真,topRadius,bottomRadius));
    }

    公众可绘制createListWithSelectedState(布尔isSelected,浮topRadius,浮bottomRadius){

        INT TC2模型,bottomColor;

        如果(isSelected){
            TC2模型= topColorSelected;
            bottomColor = bottomColorSelected;
        }其他 {
            TC2模型= topColorUnselected;
            bottomColor = bottomColorUnselected;
        }

        INT X = 10;
        诠释Y = 10;
        INT宽度= 20;
        INT高= 20;


        RoundRectShape _rrsTopShape =新RoundRectShape(getRadii(topRadius,m_nZERO_RADIUS),NULL,NULL);
        CustomShapeDrawable _csdTopShape =新CustomShapeDrawable(_rrsTopShape,TC2模型);


        RoundRectShape _rrsBottomShape =新RoundRectShape(getRadii(m_nZERO_RADIUS,bottomRadius),NULL,NULL);
        CustomShapeDrawable _csdBottomShape =新CustomShapeDrawable(_rrsBottomShape,bottomColor);
        _csdBottomShape.setBounds(X,Y,X +宽度,Y +高度);

        返回新LayerDrawable(新绘制对象[] {_csdTopShape,_csdBottomShape});
    }


    私人浮法[] getRadii(浮顶,浮底)
    {
        返回新浮法[] {顶,顶,//
                顶,顶,//
                底部,底部,//
                底部,底部//

        };
    }
    类CustomShapeDrawable扩展ShapeDrawable {
        私人最终涂料fillpaint;

        公共CustomShapeDrawable(形状S,INT补){
            超(S);
            fillpaint =新的油漆(this.getPaint());
            fillpaint.setColor(补);
        }

        @覆盖
        保护无效的OnDraw(形状的形状,帆布油画,漆漆){
            shape.draw(帆布,fillpaint);
        }
    }
}
 

解决方案 求教PS高手,这个效果怎么做

您正在寻找 LayerDrawable 的setLayerInset为了能够设置上述的另一个矩形。

下面

请参阅:

 浮法半径= 5.0F;

浮[] m_arrfTopHalfOuterRadii =新的浮动[] {半径,半径,半径,半径,0,0,0,0};
浮[] m_arrfBottomHalfOuterRadii =新的浮动[] {0,0,0,0,半径,半径,半径,半径};

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

INT m_nCellHeight = 40;

公众可绘制drawbg()
{
    RoundRectShape top_round_rect =新RoundRectShape(m_arrfTopHalfOuterRadii,NULL,NULL);
    ShapeDrawable top_shape_drawable =新ShapeDrawable(top_round_rect);
    。top_shape_drawable.getPaint()setColor(m_nTopColor);

    RoundRectShape bottom_round_rect =新RoundRectShape(m_arrfBottomHalfOuterRadii,NULL,NULL);
    ShapeDrawable bottom_shape_drawable =新ShapeDrawable(bottom_round_rect);
    。bottom_shape_drawable.getPaint()setColor(m_nBottomColor);


    绘制对象[] drawarray = {top_shape_drawable,bottom_shape_drawable};
    LayerDrawable layerdrawable =新LayerDrawable(drawarray);

    INT _nHalfOfCellHeight = m_nCellHeight / 2;
    layerdrawable.setLayerInset(0,0,0,0,_nHalfOfCellHeight); //上半
    layerdrawable.setLayerInset(1,0,_nHalfOfCellHeight,0,0); //下半区

    返回layerdrawable;
}
 

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 a simple Layer list that has two rectangles; one on top of another. I would like to use this as a background for a few different views so it is important that the result scales.

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

This approach does work but I need a separate XML for each shape depending upon whether I want the top, bottom, both, or none of the corners rounded.

my current attempts at creating the same drawable have yielded nothing more than two rectangles with one on top of the other. I could not figure out how to set the positions of the rectangles. I could see no visible change regardless of what the bounds of the shape were set to. Any suggestions would be greatly appreciated.

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

解决方案

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