Skip to content

Commit 89d2001

Browse files
committed
fix: could not get correct drawable from some colors
1 parent b13f20d commit 89d2001

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

app/src/main/java/com/osfans/trime/data/theme/ColorManager.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import android.graphics.drawable.GradientDrawable
1616
import android.graphics.drawable.NinePatchDrawable
1717
import androidx.annotation.ColorInt
1818
import androidx.collection.LruCache
19+
import androidx.core.graphics.drawable.toDrawable
1920
import androidx.core.math.MathUtils
2021
import com.osfans.trime.data.base.DataManager
2122
import com.osfans.trime.data.theme.model.ColorScheme
@@ -247,13 +248,13 @@ object ColorManager {
247248
NinePatchBitmapFactory.createNinePatchDrawable(Resources.getSystem(), bitmap)
248249
}
249250
}
250-
return BitmapDrawable(Resources.getSystem(), bitmap)
251+
return bitmap.toDrawable(Resources.getSystem())
251252
} else {
252253
val color =
253254
try {
254255
ColorUtils.parseColor(value)
255256
} catch (_: Exception) {
256-
return null
257+
Color.TRANSPARENT
257258
}
258259
return GradientDrawable().apply { setColor(color) }
259260
}
@@ -271,15 +272,16 @@ object ColorManager {
271272
@ColorInt
272273
fun getColor(key: String): Int = colorCache[key] ?: resolveColor(key)
273274

275+
fun getDrawable(key: String): Drawable? = drawableCache[key] ?: resolveDrawable(key)
276+
274277
fun getDrawable(
275278
colorKey: String,
276279
borderColorKey: String? = null,
277280
borderPx: Int = 0,
278281
cornerRadius: Float = 0f,
279282
alpha: Int = 255,
280-
): Drawable? {
281-
val drawable = drawableCache[colorKey] ?: resolveDrawable(colorKey)
282-
return when (drawable) {
283+
): Drawable? =
284+
when (val drawable = getDrawable(colorKey)) {
283285
is BitmapDrawable -> drawable.also { it.alpha = MathUtils.clamp(alpha, 0, 255) }
284286
is GradientDrawable ->
285287
drawable.also {
@@ -295,7 +297,6 @@ object ColorManager {
295297
}
296298
else -> null
297299
}
298-
}
299300

300301
private val SUPPORTED_IMG_FORMATS = arrayOf(".png", ".webp", ".jpg", ".gif")
301302
}

0 commit comments

Comments
 (0)