-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathShapeDrawableExt.kt
153 lines (140 loc) · 5.57 KB
/
ShapeDrawableExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@file:JvmName("ShapeDrawableExt")
package com.shencoder.mvvmkit.ext
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.GradientDrawable.Orientation
import android.os.Build
import androidx.annotation.ColorInt
import androidx.annotation.Px
/**
*
* @author Shenben
* @date 2024/10/12 20:05
* @description
* @since
*/
@JvmOverloads
fun shapeDrawable(
shape: Int = GradientDrawable.RECTANGLE,
@ColorInt solidColor: Int = Color.TRANSPARENT,
@Px cornerRadius: Float = 0.0f,
@Px cornerTopLeftRadius: Float = cornerRadius,
@Px cornerTopRightRadius: Float = cornerRadius,
@Px cornerBottomRightRadius: Float = cornerRadius,
@Px cornerBottomLeftRadius: Float = cornerRadius,
@Px strokeWidth: Int = 0,
@ColorInt strokeColor: Int = Color.TRANSPARENT,
@Px strokeDashWidth: Float = 0.0f,
@Px strokeDashGap: Float = 0.0f,
padding: Rect? = null,
@Px width: Int = -1,
@Px height: Int = -1
): GradientDrawable {
return GradientDrawable().also { drawable ->
drawable.shape = shape
drawable.setColor(solidColor)
if (shape == GradientDrawable.RECTANGLE) {
// 仅矩形有效
drawable.cornerRadius = cornerRadius
if (cornerTopLeftRadius != cornerRadius
|| cornerTopRightRadius != cornerRadius
|| cornerBottomRightRadius != cornerRadius
|| cornerBottomLeftRadius != cornerRadius
) {
drawable.cornerRadii = floatArrayOf(
cornerTopLeftRadius,
cornerTopLeftRadius,
cornerTopRightRadius,
cornerTopRightRadius,
cornerBottomRightRadius,
cornerBottomRightRadius,
cornerBottomLeftRadius,
cornerBottomLeftRadius
)
}
}
if (strokeWidth > 0 && strokeColor != Color.TRANSPARENT) {
drawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap)
}
if (padding != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
drawable.setPadding(padding.left, padding.top, padding.right, padding.bottom)
}
drawable.setSize(width, height)
}
}
@JvmOverloads
fun gradientDrawable(
shape: Int = GradientDrawable.RECTANGLE,
colors: IntArray? = null,
orientation: Orientation = Orientation.TOP_BOTTOM,
gradientType: Int = GradientDrawable.LINEAR_GRADIENT,
gradientCenterX: Float = 0.5f,
gradientCenterY: Float = 0.5f,
@Px gradientRadius: Float? = null,
@Px cornerRadius: Float = 0.0f,
@Px cornerTopLeftRadius: Float = cornerRadius,
@Px cornerTopRightRadius: Float = cornerRadius,
@Px cornerBottomRightRadius: Float = cornerRadius,
@Px cornerBottomLeftRadius: Float = cornerRadius,
@Px strokeWidth: Int = 0,
@ColorInt strokeColor: Int = Color.TRANSPARENT,
@Px strokeDashWidth: Float = 0.0f,
@Px strokeDashGap: Float = 0.0f,
padding: Rect? = null,
@Px width: Int = -1,
@Px height: Int = -1
): GradientDrawable {
return GradientDrawable().also { drawable ->
drawable.shape = shape
drawable.orientation = orientation
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val useCenter = colors?.size == 3
var offsets: FloatArray? = null
if (useCenter && gradientType == GradientDrawable.LINEAR_GRADIENT) {
offsets = FloatArray(3)
offsets[0] = 0.0f
// Since 0.5f is default value, try to take the one that isn't 0.5f
offsets[1] = if (gradientCenterX != 0.5f) gradientCenterX else gradientCenterY
offsets[2] = 1f
}
drawable.setColors(colors, offsets)
} else {
// Android 9及以下版本无法设置 offsets,gradientType == GradientDrawable.LINEAR_GRADIENT且需要兼容Android 9及以下版本时,需要使用xml
drawable.colors = colors
}
drawable.gradientType = gradientType
// gradientType != GradientDrawable.LINEAR_GRADIENT 时,无意义
drawable.setGradientCenter(gradientCenterX, gradientCenterY)
if (gradientType == GradientDrawable.RADIAL_GRADIENT && gradientRadius != null) {
drawable.gradientRadius = gradientRadius
}
if (shape == GradientDrawable.RECTANGLE) {
// 仅矩形有效
drawable.cornerRadius = cornerRadius
if (cornerTopLeftRadius != cornerRadius
|| cornerTopRightRadius != cornerRadius
|| cornerBottomRightRadius != cornerRadius
|| cornerBottomLeftRadius != cornerRadius
) {
drawable.cornerRadii = floatArrayOf(
cornerTopLeftRadius,
cornerTopLeftRadius,
cornerTopRightRadius,
cornerTopRightRadius,
cornerBottomRightRadius,
cornerBottomRightRadius,
cornerBottomLeftRadius,
cornerBottomLeftRadius
)
}
}
if (strokeWidth > 0 && strokeColor != Color.TRANSPARENT) {
drawable.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap)
}
if (padding != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
drawable.setPadding(padding.left, padding.top, padding.right, padding.bottom)
}
drawable.setSize(width, height)
}
}