ShapeAppearanceModel
定义自定义的 CornerTreatment
来应用于组件。类似这样: val radius = resources.getDimension(R.dimen.default_corner_radius)
val title_layout = findViewById<LinearLayout>(R.id.title_layout)
val titleShapeModel = ShapeAppearanceModel().toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED, radius)
.setTopRightCorner(CornerFamily.ROUNDED, radius)
.setBottomLeftCorner(ConcaveRoundedCornerTreatment()).setBottomLeftCornerSize(radius)
.setBottomRightCorner(ConcaveRoundedCornerTreatment()).setBottomRightCornerSize(radius)
.build()
val titleBackground = MaterialShapeDrawable(titleShapeModel)
titleBackground.setStroke(1f, ContextCompat.getColor(this, R.color.colorPrimaryDark))
ViewCompat.setBackground(title_layout, titleBackground)
其中 ConcaveRoundedCornerTreatment
位于:
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
}
}
只需对描述布局执行相同操作:
如果你正在使用像CardView
这样具有内置 shapeAppearanceModel
的视图:cardView.shapeAppearanceModel = cardView.shapeAppearanceModel.toBuilder()
.setTopRightCorner(concaveRoundedCornerTreatment).
.........
.build()