在Android中使用XML形状设置绘制矩形

时间:2022-06-13 11:00:40

I've got a problem which I don't know how to solve. Please, help me if you can. In my app I have to create a custom view extended View. In this view I should draw a lot of rectangles and I create them by canvas.drawRect or canvas.drawRoundRect. It's clear. But I want to create a compound design of these rectangles (with gradients, corners, paddings and etc.) and I want to carry out these settings (gradients, corners, paddings and etc.) in XML. How can I do it? The problem is that I determine shape in XML I can use this drawable only as background but when I draw a rectangle I can't set background for rectangle. Maybe there are another way to solve the problem. Could I use the XML shape object for setting not only as background but a view object with x,y-coordinates and width, height?

我有一个问题,我不知道怎么解决。如果可以的话,请帮帮我。在我的应用中,我必须创建一个自定义视图扩展视图。在这个视图中,我应该画很多矩形,然后用canvas创建它们。绘制矩形或canvas.drawRoundRect。很明显。但是我想创建这些矩形的复合设计(带有渐变、角、衬垫等),并且我想用XML执行这些设置(渐变、角、衬垫等)。我怎么做呢?问题是,我用XML确定形状,我只能用这个可绘制的作为背景,但是当我绘制矩形时,我不能为矩形设置背景。也许还有另一种方法来解决这个问题。我是否可以使用XML shape对象不仅设置为背景,还设置为具有x、y坐标和宽度、高度的视图对象?

Edit: I can draw rectangle:

编辑:我可以绘制矩形:

canvas.drawRect(x1, y1, x2, y2, paint);

but I have rectangle settings in XML like this:

但是我有这样的XML矩形设置:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

<!-- Specify a gradient for the background -->
    <gradient
    android:angle="90"
    android:startColor="#55000066"
    android:centerColor="#FFFFFF"
    android:endColor="#55000066" />

<!-- Specify a dark blue border -->
    <stroke 
    android:width="2dp"
    android:color="#000066" />

<!-- Specify the margins that all content inside the drawable must adhere to -->
    <padding
    android:left="5dp"
    android:right="5dp"
    android:top="5dp"
    android:bottom="5dp" />

<corners
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp" />
</shape>

and I want to apply this settings to my rectangle. How?

我想把这个设置应用到矩形上。如何?

1 个解决方案

#1


5  

You can load and use the XML defined drawable from code like so:

您可以从如下代码加载并使用XML定义的drawable:

public class CustomView extends View {

    Drawable shape;

    public CustomView(Context context) {
        super(context);
        shape = context.getResources().getDrawable(R.drawable.shape);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        shape.setBounds(left, top, right, bottom);
        shape.draw(canvas)
    }

    // ... Additional methods omitted for brevity

}

#1


5  

You can load and use the XML defined drawable from code like so:

您可以从如下代码加载并使用XML定义的drawable:

public class CustomView extends View {

    Drawable shape;

    public CustomView(Context context) {
        super(context);
        shape = context.getResources().getDrawable(R.drawable.shape);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        shape.setBounds(left, top, right, bottom);
        shape.draw(canvas)
    }

    // ... Additional methods omitted for brevity

}