使用材料零部件库,您可以定义CornerTreatment
应用于零部件。有一些内置的CornerTreatment
,例如CornerTreatment
或CutCornerTreatment
,但您可以构建自己的CornerTreatment。
类似:
CutCornerTreatment
然后将其应用于按钮:
RoundedCornerTreatment
with:
RoundedCornerTreatment
class ConcaveRoundedCornerTreatment : CornerTreatment() {
override fun getCornerPath(
shapePath: ShapePath,
angle: Float,
interpolation: Float,
radius: Float
) {
val interpolatedRadius = radius * interpolation
shapePath.reset(0f, interpolatedRadius, ANGLE_LEFT, ANGLE_LEFT - angle)
shapePath.addArc(
-interpolatedRadius,
-interpolatedRadius,
interpolatedRadius,
interpolatedRadius,
ANGLE_BOTTOM,
-angle
)
}
companion object {
const val ANGLE_LEFT = 180f
const val ANGLE_BOTTOM = 90f
}
}
创建这样的可绘制形状:
<com.google.android.material.button.MaterialButton
android:id="@+id/concave"
app:cornerRadius="16dp"
..>
然后将此可绘制对象设置为视图的背景(例如:linearlayout)注意:请根据需要调整尺寸