美文网首页
Android圆角背景设置

Android圆角背景设置

作者: 因为我的心 | 来源:发表于2022-05-30 18:06 被阅读0次

一、前言:

image.png

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

1、xml中设置圆角背景

<layout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools">

 <data>

 <import type="com.weifeng.hobby.commonui.R" />
 <import type="android.graphics.drawable.GradientDrawable"/>

 <variable
 name="vm"
 type="com.weifeng.hobby.publish.share.ShareOnSiteViewModel" />

 </data>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/ui_white">

     <RelativeLayout
            android:layout_width="@dimen/ui_dp300"
            android:layout_height="@dimen/ui_dp300"
            app:bgRadius="@{R.dimen.ui_dp8}"
            app:bgStrokeColor="@{R.color.cui_color_FF446E}"
            app:bgStrokeWidth="@{R.dimen.ui_dp5}"
            app:bgGradientStartColor="@{R.color.cui_color_46A0E0}"
            app:bgGradientEndColor="@{R.color.cui_color_46A0E0}"
app:bgGradientOrientation="@{GradientDrawable.Orientation.LEFT_RIGHT}"
            >

            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="中国加油啊!"
                android:layout_centerInParent="true"
                android:gravity="center"
                app:bgRadius="@{R.dimen.ui_dp8}"
                app:bgSolidColor="@{R.color.cui_color_87F7FC}"
                />

        </RelativeLayout>
    </RelativeLayout>

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

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

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圆角背景设置

      本文链接:https://www.haomeiwen.com/subject/pkooprtx.html