如何使不规则形状的组件可点击? Android

问题描述 投票:0回答:2

我想制作一个具有这种不规则形状的可点击组件,但是我不知道该怎么做,我已经找了几天,但没有任何解决方案。组件内部应有文本和图像。

enter image description here

有任何建议吗?

谢谢

android button
2个回答
1
投票

使用材料零部件库,您可以定义CornerTreatment应用于零部件。有一些内置的CornerTreatment,例如CornerTreatmentCutCornerTreatment,但您可以构建自己的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 } }


0
投票

创建这样的可绘制形状:

<com.google.android.material.button.MaterialButton
    android:id="@+id/concave"
    app:cornerRadius="16dp"
    ..>

然后将此可绘制对象设置为视图的背景(例如:linearlayout)注意:请根据需要调整尺寸

© www.soinside.com 2019 - 2024. All rights reserved.