-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.kt
171 lines (162 loc) · 5.12 KB
/
MainActivity.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package earth.darkwhite.dynamicblureffect
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Picture
import android.os.Build
import android.os.Bundle
import android.renderscript.Allocation
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.draw
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import earth.darkwhite.dynamicblureffect.ui.theme.DynamicBlurEffectTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DynamicBlurEffectTheme {
DynamicBlur()
}
}
}
}
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun DynamicBlur() {
val picture = remember { Picture() }
var onPictureDraw by remember { mutableStateOf(false) }
var offset by remember { mutableStateOf(IntOffset(0, 0)) }
val context = LocalContext.current
Box(modifier = Modifier.fillMaxSize()) {
Image(
painter = painterResource(id = R.drawable.a), contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.fillMaxSize()
// 1
.drawWithCache {
onDrawWithContent {
val pictureCanvas =
Canvas(
picture.beginRecording(
size.width.toInt(),
size.height.toInt()
)
)
draw(this, this.layoutDirection, pictureCanvas, this.size) {
this@onDrawWithContent.drawContent()
}
picture.endRecording()
onPictureDraw = true
drawIntoCanvas { canvas -> canvas.nativeCanvas.drawPicture(picture) }
}
}
.pointerInteropFilter { motion ->
offset = IntOffset(motion.x.toInt(), motion.y.toInt())
true
}
)
if (onPictureDraw) { // wait until picture is fully drawn
// 2
val bitmap = createBitmapFromPicture(picture)
// 3
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { // For backward compatibility
val blurredBitmap = legacyBlurImage(
bitmap = bitmap,
context = context,
blurRadio = 15f,
// blurLayer = 1, // if more blur needed, uncomment and tweak it
)
BlurImage(
modifier = Modifier,
bitmap = blurredBitmap,
offset = offset
)
} else {
BlurImage(
modifier = Modifier.blur(25.dp),
bitmap = bitmap,
offset = offset
)
}
}
}
}
@Composable
private fun BlurImage(
modifier: Modifier = Modifier,
bitmap: Bitmap,
offset: IntOffset
) {
Canvas(modifier = modifier.fillMaxSize()) {
val blurBoxSize = 200.dp.toPx().toInt()
drawImage(
image = bitmap.asImageBitmap(),
srcOffset = offset,
srcSize = IntSize(blurBoxSize, blurBoxSize),
dstOffset = offset
)
}
}
@Suppress("DEPRECATION")
private fun legacyBlurImage(
bitmap: Bitmap,
context: Context,
blurRadio: Float = 25f,
blurLayer: Int = 1,
): Bitmap {
val blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
val renderScript = RenderScript.create(context)
for (i in 0 until blurLayer) {
val bitmapAlloc = Allocation.createFromBitmap(renderScript, blurredBitmap)
ScriptIntrinsicBlur.create(renderScript, bitmapAlloc.element).apply {
setRadius(blurRadio)
setInput(bitmapAlloc)
forEach(bitmapAlloc)
}
bitmapAlloc.copyTo(blurredBitmap)
}
renderScript.destroy()
return blurredBitmap
}
private fun createBitmapFromPicture(picture: Picture): Bitmap {
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Bitmap.createBitmap(picture)
} else {
val bitmap = Bitmap.createBitmap(
picture.width,
picture.height,
Bitmap.Config.ARGB_8888
)
val canvas = android.graphics.Canvas(bitmap)
canvas.drawColor(android.graphics.Color.WHITE)
canvas.drawPicture(picture)
bitmap
}
return bitmap
}