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
}

标注效果

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

源码下载


————————————————
版权声明:本文为CSDN博主「xiangxiongfly915」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_14876133/article/details/122063689

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值