android 在自定义可绘制对象 xml 文件中创建椭圆形

android create oval inside custom drawable xml file

我成功创建了自定义 xml 可绘制文件(图像)。我在我的布局中使用了它 这是我的源代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#1Affffff" />
<stroke
    android:width="1dp"
    android:color="#80ffffff" />


<corners
    android:bottomLeftRadius="4dip"
    android:bottomRightRadius="4dip"
    android:topLeftRadius="4dip"
    android:topRightRadius="4dip" />

现在我想在中心位置添加椭圆对象,但我不知道如何在中心位置添加它。 没有人知道我如何将它添加到中心位置吗? 谢谢大家

像这样创建一个椭圆形可绘制对象

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/myColor" />
</shape>

选项 1

如果您想在该矩形的中心添加一个椭圆形,只需创建另一个椭圆形可绘制对象加载到图像视图中并将图像视图定位在呈现矩形可绘制对象的图像视图的中心

选项 2

首先从 2 个可绘制对象创建位图

Bitmap rectange = BitmapFactory.decodeResource(getResources(), R.drawable.rectangle);
Bitmap oval = BitmapFactory.decodeResource(getResources(), R.drawable.oval);

然后创建 canvas 并绘制位图,使用 drawBitmap 的输入来正确定位椭圆。

// This bitmap will be the bitmap with oval in rectange after
// you finish drawing the canvas attached to it.
Bitmap ovalInRectangle = Bitmap.createBitmap(rectangle.getWidth, rectangle.getHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ovalInRectangle);
canvas.drawBitmap(rectangle, 0, 0, null);
canvas.drawBitmap(oval, 0,0,null);
return ovalInRectangle;