Android MaterialShapeDrawable

Android MaterialShapeDrawable

概述

方便开发人员给控件添加样式。

属性说明

  • CornerTreatment:角样式

    • RoundedCornerTreatment:圆角样式
    • CutCornerTreatment:切角样式
  • EdgeTreatment:边样式

    • TriangleEdgeTreatment:三角形

    • MarkerEdgeTreatment:标记点

    • OffsetEdgeTreatment:偏移

基本使用

圆角效果

在这里插入图片描述

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCorners(RoundedCornerTreatment())
    .setAllCornerSizes(dp2px(10F))
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.GRAY)
    paintStyle = Paint.Style.FILL
}
textView01.background = backgroundDrawable

切角效果

在这里插入图片描述

private fun test6() {
    val shapeModel = ShapeAppearanceModel.builder()
        .setAllCorners(CutCornerTreatment())
        .setAllCornerSizes(dp2px(10F))
        .build()
    val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
        setTint(Color.GRAY)
        paintStyle = Paint.Style.FILL
    }
    textView06.background = backgroundDrawable
}

标注效果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2nfk84eR-1640074729035)(C:\Users\NINGMEI\AppData\Roaming\Typora\typora-user-images\image-20211221154351180.png)]

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCorners(RoundedCornerTreatment())
    .setAllCornerSizes(dp2px(10F))
    .setAllEdges(MarkerEdgeTreatment(dp2px(10F)))
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.GRAY)
    paintStyle = Paint.Style.FILL_AND_STROKE
    strokeWidth = dp2px(2F)
    strokeColor = ContextCompat.getColorStateList(context, R.color.red)
}
textView07.background = backgroundDrawable

内边三角形

在这里插入图片描述

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCorners(RoundedCornerTreatment())
    .setAllCornerSizes(dp2px(10F))
    .setAllEdges(TriangleEdgeTreatment(dp2px(10F), true))
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.GRAY)
    paintStyle = Paint.Style.FILL_AND_STROKE
    strokeWidth = dp2px(2F)
    strokeColor = ContextCompat.getColorStateList(context, R.color.red)
}
textView02.background = backgroundDrawable

外边三角形

在这里插入图片描述

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCorners(RoundedCornerTreatment())
    .setAllCornerSizes(dp2px(10F))
    .setAllEdges(TriangleEdgeTreatment(dp2px(10F), false))
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.GRAY)
    paintStyle = Paint.Style.FILL_AND_STROKE
    strokeWidth = dp2px(2F)
    strokeColor = ContextCompat.getColorStateList(context, R.color.red)
}
(textView03.parent as ViewGroup).clipChildren = false
textView03.background = backgroundDrawable

气泡

在这里插入图片描述

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCornerSizes(dp2px(6F))
    .setBottomEdge(
        OffsetEdgeTreatment(
            TriangleEdgeTreatment(dp2px(6F), false),
            dp2px(30F)
        )
    )
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.parseColor("#FA4B05"))
    paintStyle = Paint.Style.FILL
}
(textView04.parent as ViewGroup).clipChildren = false
textView04.background = backgroundDrawable

在这里插入图片描述

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCornerSizes(dp2px(6F))
    .setRightEdge(
        OffsetEdgeTreatment(
            TriangleEdgeTreatment(dp2px(6F), false),
            dp2px(0F)
        )
    )
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.parseColor("#FA4B05"))
    paintStyle = Paint.Style.FILL
}
(textView05.parent as ViewGroup).clipChildren = false
textView05.background = backgroundDrawable

阴影

在这里插入图片描述

val shapeModel = ShapeAppearanceModel.builder()
    .setAllCorners(RoundedCornerTreatment())
    .setAllCornerSizes(dp2px(16F))
    .build()
val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
    setTint(Color.GRAY)
    paintStyle = Paint.Style.FILL

    //绘制阴影
    shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
    initializeElevationOverlay(context)
    setShadowColor(Color.RED)
    elevation = dp2px(10F)
}
(textView08.parent as ViewGroup).clipChildren = false
textView08.background = backgroundDrawable

源码下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值