@@ -3,12 +3,24 @@ package com.bumptech.glide.integration.compose
3
3
import android.graphics.drawable.BitmapDrawable
4
4
import android.graphics.drawable.ColorDrawable
5
5
import android.graphics.drawable.Drawable
6
+ import android.os.Build
7
+ import android.view.View
8
+ import androidx.compose.ui.geometry.Size
6
9
import androidx.compose.ui.graphics.Color
10
+ import androidx.compose.ui.graphics.ColorFilter
11
+ import androidx.compose.ui.graphics.asAndroidColorFilter
7
12
import androidx.compose.ui.graphics.asImageBitmap
13
+ import androidx.compose.ui.graphics.drawscope.DrawScope
14
+ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
15
+ import androidx.compose.ui.graphics.nativeCanvas
8
16
import androidx.compose.ui.graphics.painter.BitmapPainter
9
17
import androidx.compose.ui.graphics.painter.ColorPainter
10
18
import androidx.compose.ui.graphics.painter.Painter
11
- import com.google.accompanist.drawablepainter.DrawablePainter
19
+ import androidx.compose.ui.graphics.withSave
20
+ import androidx.compose.ui.unit.IntSize
21
+ import androidx.compose.ui.unit.LayoutDirection
22
+ import androidx.compose.ui.unit.toSize
23
+ import kotlin.math.roundToInt
12
24
13
25
internal fun Drawable?.toPainter (): Painter =
14
26
when (this ) {
@@ -17,3 +29,59 @@ internal fun Drawable?.toPainter(): Painter =
17
29
null -> ColorPainter (Color .Transparent )
18
30
else -> DrawablePainter (mutate())
19
31
}
32
+
33
+ private class DrawablePainter (
34
+ val drawable : Drawable
35
+ ) : Painter() {
36
+ init {
37
+ if (drawable.isIntrinsicSizeValid) {
38
+ drawable.setBounds(0 , 0 , drawable.intrinsicWidth, drawable.intrinsicHeight)
39
+ }
40
+ }
41
+
42
+ private var drawableIntrinsicSize = drawable.intrinsicSize
43
+
44
+ private val Drawable .isIntrinsicSizeValid
45
+ get() = intrinsicWidth >= 0 && intrinsicHeight >= 0
46
+
47
+ private val Drawable .intrinsicSize: Size
48
+ get() = if (isIntrinsicSizeValid) {
49
+ IntSize (intrinsicWidth, intrinsicHeight).toSize()
50
+ } else {
51
+ Size .Unspecified
52
+ }
53
+
54
+ override fun applyAlpha (alpha : Float ): Boolean {
55
+ drawable.alpha = (alpha * 255 ).roundToInt().coerceIn(0 , 255 )
56
+ return true
57
+ }
58
+
59
+ override fun applyColorFilter (colorFilter : ColorFilter ? ): Boolean {
60
+ drawable.colorFilter = colorFilter?.asAndroidColorFilter()
61
+ return true
62
+ }
63
+
64
+ override fun applyLayoutDirection (layoutDirection : LayoutDirection ): Boolean {
65
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
66
+ return drawable.setLayoutDirection(
67
+ when (layoutDirection) {
68
+ LayoutDirection .Ltr -> View .LAYOUT_DIRECTION_LTR
69
+ LayoutDirection .Rtl -> View .LAYOUT_DIRECTION_RTL
70
+ }
71
+ )
72
+ }
73
+ return false
74
+ }
75
+
76
+ override val intrinsicSize: Size get() = drawableIntrinsicSize
77
+
78
+ override fun DrawScope.onDraw () {
79
+ drawIntoCanvas { canvas ->
80
+ drawable.setBounds(0 , 0 , size.width.roundToInt(), size.height.roundToInt())
81
+
82
+ canvas.withSave {
83
+ drawable.draw(canvas.nativeCanvas)
84
+ }
85
+ }
86
+ }
87
+ }
0 commit comments