我怎样才能创建一个矩形在XML可绘制的两个曲面?

我想要矩形的左侧和右侧(而不是角落)是弯曲的。 或者说,椭圆形的顶部和底部的直线。

我怎么能做到这样的事情?

在这里输入图像描述

在textview上试试这个,单个字符会显示一个圆圈,对于多个数字,它会自动展开成上面显示的形状,但是如果您严格要求上面的形状,只需在左侧和右侧填充较大的填充

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <padding android:top="3dp" android:left="8dp" android:right="8dp" android:bottom="3dp"/> <solid android:color="#E6121A" /> <corners android:bottomLeftRadius="12dp" android:bottomRightRadius="12dp" android:topLeftRadius="12dp" android:topRightRadius="12dp" /> </shape> 

我迟到了,答案不能完全完成(我不考虑弹性高度),但至少如果我们事先知道dp的高度,那么窍门就是将半径作为按钮高度的一半。 例如,如果高度是48dp,我们可以做一些事情:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ff0000"/> <corners android:radius="24dp" /> </shape> 

我认为最好的想法之一是使用XMLFILES创建形状。

创建Drawable-> ovalshape.xml

 <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ff0000" /> <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp" /> <padding android:bottom="5dip" android:left="10dip" android:right="10dip" android:top="5dip" /> </shape> 

现在你可以使用这个xml instead of image容易。我认为这对you and new SO user有帮助。

尝试将边框半径设置为高度的一半。 在CSS中, border-radius:50%创建一个椭圆,所以我猜如果它只有50%的高度,那么你会得到类似的东西。

简单的方法是使用一个9-patch的图像 (一个以image.9.png结尾的png图像),并有一个额外的像素边界,用于定义如何缩放图像。

另一种方法是在res / drawable文件夹中创建一个形状文件就像那样。

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#ff0000"/> </shape> 

有关形状的更多信息

看起来形状所允许的最大半径是波纹形状的总高度的一半,所以你可以用它来形成一个高度灵活的形状来保持欲望比例:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ffffff" /> <size android:height="@dimen/height" /> <corners android:radius="@dimen/height" /> </shape> 

定义高度并使半径为高度的一半。

它工作正常!

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/color_red"/> <corners android:radius="10000dp" /> </shape> 

您可以将半径增加到200dp,并将drawable设置为textview的背景

 <solid android:color="#E6121A" /> <corners android:bottomLeftRadius="12dp" android:bottomRightRadius="12dp" android:topLeftRadius="12dp" android:topRightRadius="12dp" /> 

为了使双方始终保持弯曲与任何高度,我最终不得不以编程方式创建形状像下面(代码在Kotlin)

 class CurvedSidesShape : RectShape() { override fun draw(canvas: Canvas, paint: Paint?) { var path = Path() path.addRoundRect(rect(), rect().height(), rect().height(), Path.Direction.CW) canvas.drawPath(path, paint) } } 

这里是我如何使用形状作为按钮背景

 class CurvedSidesButton : Button { private var mHeight: Int = 0 constructor(context: Context?) : super(context) { init(context, null, 0, 0) } . . . override fun draw(canvas: Canvas?) { setCurvedSidesBackground(height) super.draw(canvas) } private fun setCurvedSidesBackground(height: Int) { if (height != mHeight) { val curvedSidesShape = ShapeDrawable(CurvedSidesShape()) curvedSidesShape.intrinsicWidth = width curvedSidesShape.intrinsicHeight = height curvedSidesShape.paint.color = Color.RED background = curvedSidesShape mHeight = height } } }