Skip to content

Instantly share code, notes, and snippets.

@ruthwikkk
ruthwikkk / BlurSurface.kt
Created October 7, 2023 13:36
Final Code for BlurSurface
@Composable
fun BlurSurface(
modifier: Modifier,
parent: ViewGroup
) {
Surface(
modifier = modifier,
color = Color.Transparent
) {
AndroidView(
@ruthwikkk
ruthwikkk / BlurSurfaceView_04.kt
Created October 7, 2023 12:56
Code for RenderScript blur
private fun blurWithRenderScript() {
val renderScript = RenderScript.create(context)
val blurScript = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript))
val inAllocation = Allocation.createFromBitmap(renderScript, bitmap)
val outAllocation = Allocation.createTyped(renderScript, inAllocation.type)
blurScript.setRadius(20f)
blurScript.setInput(inAllocation)
blurScript.forEach(outAllocation)
@ruthwikkk
ruthwikkk / BlurSurfaceView_03.kt
Created October 7, 2023 12:11
Code for finding relative position and translate the draw pointer
fun getBackgroundAndDrawBehind(parent: ViewGroup) {
//Arrays to store the co-ordinates
val rootLocation = IntArray(2)
val viewLocation = IntArray(2)
parent.getLocationOnScreen(rootLocation) //get the parent co-ordinates
this.getLocationOnScreen(viewLocation) //get view co-ordinates
//Calculate relative co-ordinates
val left: Int = viewLocation[0] - rootLocation[0]
@ruthwikkk
ruthwikkk / MainActivity_03.kt
Created October 7, 2023 11:43
Call this method on the update callback of AndroidView
AndroidView(
factory = {
BlurSurfaceView(context)
},
modifier = Modifier
.size(180.dp),
update = { blurView ->
blurView.getBackgroundAndDrawBehind(
parent = rootView //Pass rootView here
@ruthwikkk
ruthwikkk / BlurSurfaceView_02.kt
Created October 7, 2023 11:34
Function to capture and draw background
fun getBackgroundAndDrawBehind(parent: ViewGroup) {
canvas?.save()
canvas?.let {
parent.draw(it)
}
canvas?.restore()
}
@ruthwikkk
ruthwikkk / MainActivity_02.kt
Created October 7, 2023 11:25
Code to get root view from Activity
val decorView = window.decorView
val rootView = decorView.findViewById(android.R.id.content) as ViewGroup
@ruthwikkk
ruthwikkk / MainActivity_01.kt
Created October 7, 2023 10:52
Add custom view to main layout
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Theme {
Surface(
modifier = Modifier.fillMaxSize(),
color = Color.Black
) {
@ruthwikkk
ruthwikkk / BlurSurfaceView_01.kt
Last active October 7, 2023 10:50
Create base custom view
class BlurSurfaceView: View {
private var canvas: Canvas? = null
private lateinit var bitmap: Bitmap
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)