我希望矩形的左侧和右侧(不是角落)是弯曲的,或者说椭圆形的顶部和底部是直的。
我该如何实现这样的效果?
我希望矩形的左侧和右侧(不是角落)是弯曲的,或者说椭圆形的顶部和底部是直的。
我该如何实现这样的效果?
在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>
它工作得很好!
<?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>
我有点迟到了,这个答案可能不完全(我并没有考虑到灵活的高度),但至少如果我们提前知道以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>
<?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代替图片
。我认为这对于你和新的SO用户
很有帮助。看起来在下面的形状中允许的最大半径是总高度的一半,因此您可以使用它来获得具有灵活高度并保持所需比例的形状:
<?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>
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
}
}
}
最简单的方法是使用9-patch image(一种以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>
关于形状的更多信息在这里
定义高度并使半径为高度的一半。
border-radius:50%
会创建一个椭圆形,因此我猜如果它只有高度的50%,那么你会得到类似的东西。<solid android:color="#E6121A" />
<corners
android:bottomLeftRadius="12dp"
android:bottomRightRadius="12dp"
android:topLeftRadius="12dp"
android:topRightRadius="12dp" />