token自定义增加色值,宽度直接设置模式

This commit is contained in:
renhaoting 2025-10-30 10:40:54 +08:00
parent cfee7191c8
commit f30db51681
6 changed files with 204 additions and 18 deletions

View File

@ -181,24 +181,41 @@ fun View.changeBackground(
*/ */
fun View.changeBackground(customViewToken: CustomViewToken) { fun View.changeBackground(customViewToken: CustomViewToken) {
customViewToken.run { customViewToken.run {
setTokenBgColor( if (advRadius > 0 || advStrokeWidth > 0 ||
normalToken = backgroundUIColorToken, advTopRightRadius > 0 || advTopLeftRadius > 0 ||
pressedToken = backgroundUIPressedColorToken, advBottomLeftRadius > 0 || advBottomRightRadius > 0) {
hoveredToken = backgroundUIHoveredColorToken, setBgColorDirectly(
disabledToken = backgroundUIDisabledColorToken, bgColor = advBgColor,
radiusToken = radiusToken, radius = advRadius,
topLeftRadiusToken = topLeftRadiusToken.ifEmpty { radiusToken }, topLeftRadius = if (advTopLeftRadius > 0F) advTopLeftRadius else advRadius,
topRightRadiusToken = topRightRadiusToken.ifEmpty { radiusToken }, topRightRadius = if (advTopRightRadius > 0F) advTopRightRadius else advRadius,
bottomRightRadiusToken = bottomRightRadiusToken.ifEmpty { radiusToken }, bottomRightRadius = if (advBottomRightRadius > 0F) advBottomRightRadius else advRadius,
bottomLeftRadiusToken = bottomLeftRadiusToken.ifEmpty { radiusToken }, bottomLeftRadius = if (advBottomLeftRadius > 0F) advBottomLeftRadius else advRadius,
strokeColorWidthToken = strokeUIWidthToken, strokeWidth = advStrokeWidth,
normalStrokeColorToken = strokeUIColorToken, strokeColor = advStrokeColor,
pressedStrokeColorToken = strokeUIPressedColorToken, )
hoveredStrokeColorToken = strokeUIHoveredColorToken,
disabledStrokeColorToken = strokeUIDisabledColorToken, } else {
dashWidth = strokeDashWidth, setTokenBgColor(
dashGap = strokeDashGap, normalToken = backgroundUIColorToken,
) pressedToken = backgroundUIPressedColorToken,
hoveredToken = backgroundUIHoveredColorToken,
disabledToken = backgroundUIDisabledColorToken,
radiusToken = radiusToken,
topLeftRadiusToken = topLeftRadiusToken.ifEmpty { radiusToken },
topRightRadiusToken = topRightRadiusToken.ifEmpty { radiusToken },
bottomRightRadiusToken = bottomRightRadiusToken.ifEmpty { radiusToken },
bottomLeftRadiusToken = bottomLeftRadiusToken.ifEmpty { radiusToken },
strokeColorWidthToken = strokeUIWidthToken,
normalStrokeColorToken = strokeUIColorToken,
pressedStrokeColorToken = strokeUIPressedColorToken,
hoveredStrokeColorToken = strokeUIHoveredColorToken,
disabledStrokeColorToken = strokeUIDisabledColorToken,
dashWidth = strokeDashWidth,
dashGap = strokeDashGap,
)
}
} }
} }
@ -491,3 +508,67 @@ fun View.getGradientDrawable(
) )
return gradientDrawable return gradientDrawable
} }
//*------------------------ new added ------------------------*//
/**
* 设置背景色
*/
fun View.setBgColorDirectly(
bgColor: Int = 0,
radius: Float = 0F,
topLeftRadius: Float = radius,
topRightRadius: Float = radius,
bottomRightRadius: Float = radius,
bottomLeftRadius: Float = radius,
strokeWidth: Float = 0F,
strokeColor: Int = -1
) {
val resDrawable = StateListDrawable()
val normalDrawable = getGradientDrawableDirectly(
bgColor,
topLeftRadius,
topRightRadius,
bottomRightRadius,
bottomLeftRadius,
strokeWidth,
strokeColor
)
resDrawable.addState(intArrayOf(android.R.attr.state_enabled), normalDrawable)
background = resDrawable
}
fun View.getGradientDrawableDirectly(
bgColor: Int = 0,
topLeftRadius: Float = 0F,
topRightRadius: Float = 0F,
bottomRightRadius: Float = 0F,
bottomLeftRadius: Float = 0F,
strokeWidth: Float = 0F,
strokeColor: Int = -1
): Drawable {
val gradientDrawable = GradientDrawable()
gradientDrawable.shape = GradientDrawable.RECTANGLE
gradientDrawable.setColor(bgColor)
if (strokeWidth > 0) {
gradientDrawable.setStroke(strokeWidth.toInt(), strokeColor)
}
gradientDrawable.cornerRadii = floatArrayOf(
topLeftRadius,
topLeftRadius,
topRightRadius,
topRightRadius,
bottomRightRadius,
bottomRightRadius,
bottomLeftRadius,
bottomLeftRadius
)
return gradientDrawable
}

View File

@ -1,5 +1,7 @@
package com.remax.visualnovel.widget.uitoken.bean package com.remax.visualnovel.widget.uitoken.bean
import android.graphics.Color
/** /**
* Created by HJW on 2022/9/1 * Created by HJW on 2022/9/1
*/ */
@ -41,4 +43,15 @@ data class CustomViewToken(
var onlyIconFont: Boolean = false, var onlyIconFont: Boolean = false,
var fixTextIsSelectable: Boolean = false, var fixTextIsSelectable: Boolean = false,
var underline: Boolean = false, var underline: Boolean = false,
//---------------------- New added -----------------------
var advBgColor:Int = Color.WHITE,
var advRadius:Float = 0F,
var advTopLeftRadius:Float = 0F,
var advTopRightRadius:Float = 0F,
var advBottomRightRadius:Float = 0F,
var advBottomLeftRadius:Float = 0F,
var advStrokeColor: Int = Color.BLACK,
var advStrokeWidth:Float = 0F,
) )

View File

@ -77,6 +77,17 @@ open class UITokenTextView @JvmOverloads constructor(context: Context, private v
onlyIconFont = getBoolean(R.styleable.UITokenTextView_onlyIconFont, onlyIconFont) onlyIconFont = getBoolean(R.styleable.UITokenTextView_onlyIconFont, onlyIconFont)
underline = getBoolean(R.styleable.UITokenTextView_underline, underline) underline = getBoolean(R.styleable.UITokenTextView_underline, underline)
fixTextIsSelectable = getBoolean(R.styleable.UITokenTextView_fixTextIsSelectable, fixTextIsSelectable) fixTextIsSelectable = getBoolean(R.styleable.UITokenTextView_fixTextIsSelectable, fixTextIsSelectable)
//<!-- new added 2025.10.29 -->
advBgColor = getColor(R.styleable.UITokenTextView_advBgColor, advBgColor)
advRadius = getDimension(R.styleable.UITokenTextView_advRadius, advRadius)
advTopLeftRadius = getDimension(R.styleable.UITokenTextView_advTopLeftRadius, advTopLeftRadius)
advTopRightRadius = getDimension(R.styleable.UITokenTextView_advTopRightRadius, advTopRightRadius)
advBottomRightRadius = getDimension(R.styleable.UITokenTextView_advBottomRightRadius, advBottomRightRadius)
advBottomLeftRadius = getDimension(R.styleable.UITokenTextView_advBottomLeftRadius, advBottomLeftRadius)
advStrokeColor = getColor(R.styleable.UITokenTextView_advStrokeColor, advStrokeColor)
advStrokeWidth = getDimension(R.styleable.UITokenTextView_advStrokeWidth, advStrokeWidth)
} }
} }

View File

@ -23,6 +23,19 @@
android:text="@string/setting" android:text="@string/setting"
/> />
<com.remax.visualnovel.widget.uitoken.view.UITokenTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_45"
android:padding="@dimen/dp_45"
android:text="Test....Test"
app:advBgColor="@color/glo_color_blue_80"
app:advRadius="@dimen/dp_30"
app:advStrokeColor="@color/black"
app:advStrokeWidth="@dimen/dp_5"
android:gravity="center"
/>
<!-- Ai model selecting related --> <!-- Ai model selecting related -->
<com.remax.visualnovel.widget.uitoken.view.UITokenTextView <com.remax.visualnovel.widget.uitoken.view.UITokenTextView

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.remax.visualnovel.widget.uitoken.view.UITokenTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="aaaaaaaaaaaaaa"
android:padding="@dimen/dp_45"
app:advBgColor="@color/glo_color_blue_40"
app:advRadius="@dimen/dp_15"
/>
</RelativeLayout>

View File

@ -97,6 +97,17 @@
format="boolean" /> format="boolean" />
<attr name="textLinkColorToken" <attr name="textLinkColorToken"
format="string" /> format="string" />
<!-- new added 2025.10.29 -->
<attr name="advBgColor" format="color" />
<attr name="advRadius" format="dimension" />
<attr name="advTopLeftRadius" format="dimension" />
<attr name="advTopRightRadius" format="dimension" />
<attr name="advBottomRightRadius" format="dimension" />
<attr name="advBottomLeftRadius" format="dimension" />
<attr name="advStrokeColor" format="color" />
<attr name="advStrokeWidth" format="dimension" />
</declare-styleable> </declare-styleable>
<declare-styleable name="UITokenEditView"> <declare-styleable name="UITokenEditView">
@ -125,6 +136,16 @@
<attr name="outlineToken" /> <attr name="outlineToken" />
<attr name="xClickExpend" /> <attr name="xClickExpend" />
<attr name="yClickExpend" /> <attr name="yClickExpend" />
<!-- new added 2025.10.29 -->
<!--<attr name="advBgColor" format="color" />
<attr name="advRadius" format="dimension" />
<attr name="advTopLeftRadius" format="dimension" />
<attr name="advTopRightRadius" format="dimension" />
<attr name="advBottomRightRadius" format="dimension" />
<attr name="advBottomLeftRadius" format="dimension" />
<attr name="advStrokeColor" format="color" />
<attr name="advStrokeWidth" format="dimension" />-->
</declare-styleable> </declare-styleable>
<declare-styleable name="UITokenImageView"> <declare-styleable name="UITokenImageView">
@ -148,6 +169,16 @@
<attr name="outlineToken" /> <attr name="outlineToken" />
<attr name="xClickExpend" /> <attr name="xClickExpend" />
<attr name="yClickExpend" /> <attr name="yClickExpend" />
<!-- new added 2025.10.29 -->
<!--<attr name="advBgColor" format="color" />
<attr name="advRadius" format="dimension" />
<attr name="advTopLeftRadius" format="dimension" />
<attr name="advTopRightRadius" format="dimension" />
<attr name="advBottomRightRadius" format="dimension" />
<attr name="advBottomLeftRadius" format="dimension" />
<attr name="advStrokeColor" format="color" />
<attr name="advStrokeWidth" format="dimension" />-->
</declare-styleable> </declare-styleable>
<declare-styleable name="UITokenFrameLayout"> <declare-styleable name="UITokenFrameLayout">
@ -171,6 +202,16 @@
<attr name="outlineToken" /> <attr name="outlineToken" />
<attr name="xClickExpend" /> <attr name="xClickExpend" />
<attr name="yClickExpend" /> <attr name="yClickExpend" />
<!-- new added 2025.10.29 -->
<!--<attr name="advBgColor" format="color" />
<attr name="advRadius" format="dimension" />
<attr name="advTopLeftRadius" format="dimension" />
<attr name="advTopRightRadius" format="dimension" />
<attr name="advBottomRightRadius" format="dimension" />
<attr name="advBottomLeftRadius" format="dimension" />
<attr name="advStrokeColor" format="color" />
<attr name="advStrokeWidth" format="dimension" />-->
</declare-styleable> </declare-styleable>
<declare-styleable name="UITokenRelativeLayout"> <declare-styleable name="UITokenRelativeLayout">
@ -194,6 +235,16 @@
<attr name="outlineToken" /> <attr name="outlineToken" />
<attr name="xClickExpend" /> <attr name="xClickExpend" />
<attr name="yClickExpend" /> <attr name="yClickExpend" />
<!-- new added 2025.10.29 -->
<!--<attr name="advBgColor" format="color" />
<attr name="advRadius" format="dimension" />
<attr name="advTopLeftRadius" format="dimension" />
<attr name="advTopRightRadius" format="dimension" />
<attr name="advBottomRightRadius" format="dimension" />
<attr name="advBottomLeftRadius" format="dimension" />
<attr name="advStrokeColor" format="color" />
<attr name="advStrokeWidth" format="dimension" />-->
</declare-styleable> </declare-styleable>
<declare-styleable name="UITokenLinearLayout"> <declare-styleable name="UITokenLinearLayout">