美文网首页
Android 圆角设置

Android 圆角设置

作者: 全球顶尖伪极客 | 来源:发表于2022-06-06 20:52 被阅读0次

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/userHeadImage"
            android:layout_width="@dimen/ui_dp24"
            android:layout_height="@dimen/ui_dp24"
            android:layout_marginVertical="@dimen/ui_dp10"
            android:layout_marginStart="@dimen/ui_dp4"
            app:strokeColor="@color/cui_color_white"
            app:strokeWidth="@dimen/ui_dp0"
            android:layout_marginEnd="@dimen/ui_dp4"
            android:scaleType="fitXY"
            android:src="@drawable/default_user_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:shapeAppearanceOverlay="@style/star_maku_party_solitaire_circleStyle" />

 <!-- 圆形图片 -->
    <style name="star_maku_party_solitaire_circleStyle">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
    </style>

tab.customView?.findViewById<ShapeableImageView>(R.id.userHeadImage)?.apply {
            strokeWidth = resource.getDimension(R.dimen.ui_dp1)

//            strokeColor = resource.getColor(R.color.cui_color_white)
//            resource.getColorStateList(R.color.selector,context.getTheme());
//            strokeColor = MaterialResources.getColorStateList(
//                context, context.theme, R.styleable.ShapeableImageView_strokeColor)
        }
 <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/focusText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/ui_dp8"
            android:paddingHorizontal="@dimen/ui_dp8"
            android:paddingVertical="@dimen/ui_dp4"
            android:text="关注"
            android:textColor="#ff000000"
            android:textSize="12sp"
            app:bgRadius="@{R.dimen.ui_dp12}"
            app:bgSolidColor="@{R.color.cui_color_CAF349}"
            app:bgStrokeColor="@{R.color.cui_color_CAF349}"
            app:bgStrokeWidth="@{R.dimen.ui_dp0}"
            app:layout_constraintBottom_toBottomOf="@id/leftPartyInfoLayout"
            app:layout_constraintStart_toEndOf="@id/leftPartyInfoLayout"
            app:layout_constraintTop_toTopOf="@id/leftPartyInfoLayout"
            tools:background="@color/cui_color_CAF349" />


import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.RippleDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RoundRectShape
import android.view.View
import androidx.databinding.BindingAdapter

/**
 * 设置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/xgzjmrtx.html