This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun BlurSurface( | |
modifier: Modifier, | |
parent: ViewGroup | |
) { | |
Surface( | |
modifier = modifier, | |
color = Color.Transparent | |
) { | |
AndroidView( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AndroidView( | |
factory = { | |
BlurSurfaceView(context) | |
}, | |
modifier = Modifier | |
.size(180.dp), | |
update = { blurView -> | |
blurView.getBackgroundAndDrawBehind( | |
parent = rootView //Pass rootView here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getBackgroundAndDrawBehind(parent: ViewGroup) { | |
canvas?.save() | |
canvas?.let { | |
parent.draw(it) | |
} | |
canvas?.restore() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val decorView = window.decorView | |
val rootView = decorView.findViewById(android.R.id.content) as ViewGroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
Theme { | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = Color.Black | |
) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |