-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinIconPainter.kt
80 lines (65 loc) · 2.16 KB
/
WinIconPainter.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
package com.starmel.windowsapplication.ui.theme
import android.graphics.BitmapFactory
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.graphics.get
import com.starmel.windowsapplication.R
@Composable
fun rememberWindowIconPainter(@DrawableRes resId: Int): Painter {
val resources = LocalContext.current.resources
return remember {
val bitmap = BitmapFactory.decodeResource(resources, resId)
val scale = 2f
val image = ImageBitmap(
width = bitmap.width * scale.toInt(),
height = bitmap.height * scale.toInt()
)
Canvas(image).apply {
val paint = Paint().apply {
isAntiAlias = false
}
for (x in 0 until bitmap.width) {
for (y in 0 until bitmap.height) {
paint.color = Color(bitmap[x, y])
drawRect(
Rect(
Offset(x * scale, y * scale),
Size(scale, scale)
),
paint
)
}
}
save()
}
BitmapPainter(image = image)
}
}
@Composable
fun WindowsIcon(@DrawableRes resId: Int) {
Image(
painter = rememberWindowIconPainter(resId),
contentDescription = null,
)
}
@Composable
@Preview(showBackground = true)
private fun IconPreview() {
Image(
painter = rememberWindowIconPainter(R.drawable.netmeeting_0),
contentDescription = null,
)
}