Android圆角背景设置

一、前言:

image.png

使用databinding设置圆角背景,代替drawable方式

1、xml中设置圆角背景



 

 
 

 

 


    

     

            

        
    

注意:这个只是设置一个背景,所有图片的圆角不能使用它,只能是viewGroup或者TextView。
提示:图片可以使用QMUIRadiusImageView

1、支持view和viewGroup的圆角,边框、和单个圆角等;
2、app:bgRadius:圆角大小,必须用"@{R.dimen.ui_dp8}"赋值;
3、app:bgSolidColor:设置背景色;
4、app:bgStrokeColor:设置边框颜色;
5、bgTopLeftRadius:设置左上的圆角,必须用"@{R.dimen.ui_dp8}"赋值,支持XML;
6、bgBottomLeftRadius:设置左下的圆角,必须用"@{R.dimen.ui_dp8}"赋值,支持XML;

代码设置:

tvLimit.setBgParams(bgTopLeftRadius=R.dimen.ui_dp2, bgBottomLeftRadius =R.dimen.ui_dp2)
   tvLimit.setBgParams(bgTopLeftRadius=R.dimen.ui_dp2, bgBottomLeftRadius =R.dimen.ui_dp2)
      binding.rlTime.setBgParams(bgRadius = R.dimen.ui_dp14, bgSolidColor= R.color.cui_color_1A16D2D7)
            binding.tvTime.setColorX(R.color.cui_color_16D2D7)

注意:圆角和颜色必须是资源文件
注意:XML设置后,代码中就不要再次设置了

2、ViewShapeAdapter(扩展函数)

/**
 *
 * 设置view 背景
 */
object ViewShapeAdapter {

    @JvmStatic
    @BindingAdapter(
        "bgRadius",
        "bgTopLeftRadius",
        "bgTopRightRadius",
        "bgBottomLeftRadius",
        "bgBottomRightRadius",
        "bgSolidColor",
        "bgRippleColor",
        "bgStrokeWidth",
        "bgStrokeColor",
        "bgGradientStartColor",
        "bgGradientEndColor",
        "bgGradientOrientation",
        "bgShapeWidth",
        "bgShapeHeight",
        "bgEnable",
        requireAll = false
    )
    fun View.setBgParams(
        bgRadius: Int? = null,
        bgTopLeftRadius: Int? = null,
        bgTopRightRadius: Int? = null,
        bgBottomLeftRadius: Int? = null,
        bgBottomRightRadius: Int? = null,
        bgSolidColor: Int? = null,
        bgRippleColor: Int? = null,
        bgStrokeWidth: Int? = null,
        bgStrokeColor: Int? = null,
        bgGradientStartColor: Int? = null,
        bgGradientEndColor: Int? = null,
        bgGradientOrientation: GradientDrawable.Orientation? = null,
        bgShapeWidth: Int? = null,
        bgShapeHeight: Int? = null,
        bgEnable: Boolean? = null
    ) {

        if (isEffective(bgRippleColor)) {
            setRippleBg(this, bgRippleColor, bgSolidColor)
            return
        }

        val drawable = when {
            isEffective(bgGradientStartColor) && isEffective(bgGradientEndColor) -> GradientDrawable(
                bgGradientOrientation, intArrayOf(
                    resources.getColor(bgGradientStartColor!!), resources.getColor(bgGradientEndColor!!)
                )
            )
            else -> GradientDrawable().apply {
                if (isEffective(bgSolidColor)) setColor(resources.getColor(bgSolidColor!!))
            }
        }

        if (isEffective(bgStrokeWidth) && isEffective(bgStrokeColor)) {
            drawable.setStroke(
                resources.getDimension(bgStrokeWidth!!).toInt(),
                resources.getColor(bgStrokeColor!!)
            )
        }

        bgEnable?.let {
            isEnabled = it
            drawable.alpha = if (it) 225 else 68
        }

        if (bgRadius != null) {
            drawable.cornerRadius = resources.getDimension(bgRadius)
        } else if ((bgTopLeftRadius != null || bgTopRightRadius != null) || bgBottomLeftRadius != null || bgBottomRightRadius != null) {
            val topLeft = bgTopLeftRadius?.let { resources.getDimension(bgTopLeftRadius) } ?: 0f
            val topRight = bgTopRightRadius?.let { resources.getDimension(bgTopRightRadius) } ?: 0f
            val bottomLeft = bgBottomLeftRadius?.let { resources.getDimension(bgBottomLeftRadius) } ?: 0f
            val bottomRight = bgBottomRightRadius?.let { resources.getDimension(bgBottomRightRadius) } ?: 0f

            drawable.cornerRadii =
                floatArrayOf(topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft)
        } else {
            drawable.cornerRadius = 0f
        }

        if (isEffective(bgShapeWidth) && isEffective(bgShapeHeight)) {
            drawable.setSize(
                resources.getDimension(bgShapeWidth!!).toInt(), resources.getDimension(bgShapeHeight!!).toInt()
            )
        }

        background = drawable
    }

    private fun setRippleBg(view: View, bgRippleColor: Int?, bgSolidColor: Int?) {
        if (bgRippleColor == null) return

        val pressedColor = resource.getColor(bgRippleColor)
        val normalColor = if (bgSolidColor != null) resource.getColor(bgSolidColor) else Color.TRANSPARENT

        val outRadius = floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f)
        val roundRectShape = RoundRectShape(outRadius, null, null)

        val maskDrawable = ShapeDrawable()
        maskDrawable.shape = roundRectShape
        maskDrawable.paint.style = Paint.Style.FILL

        val contentDrawable = ShapeDrawable()
        contentDrawable.shape = roundRectShape
        contentDrawable.paint.color = normalColor
        contentDrawable.paint.style = Paint.Style.FILL

        val colorStateList = ColorStateList.valueOf(pressedColor)
        val rippleDrawable = RippleDrawable(colorStateList, contentDrawable, maskDrawable)
        view.background = rippleDrawable
    }


    @JvmStatic
    @BindingAdapter(
        "normalImg",
        "pressImg"
    )
    fun View.setStateBackgroundOrDrawable(normalImg: Int?, pressImg: Int?) {
        if (normalImg == null || pressImg == null) {
            return
        }
        val drawable = StateListDrawable()
        drawable.addState(intArrayOf(android.R.attr.state_pressed), resource.getDrawable(pressImg))
        drawable.addState(intArrayOf(-android.R.attr.state_checked), resource.getDrawable(normalImg))
        background = drawable
    }

    private fun isEffective(resId: Any?): Boolean {
        return resId != null && resId != 0
    }
}

你可能感兴趣的:(Android圆角背景设置)