Dimezis / BlurView

Dynamic iOS-like blur of underlying Views for Android
Apache License 2.0
3.53k stars 334 forks source link

how can i blur BottomSheetDialogFragment background? #153

Closed namnguyen211 closed 2 years ago

namnguyen211 commented 3 years ago

Please include: 1) Library version: com.eightbitlab:blurview:1.6.6

Please, help me blur BottomSheetDialogFragment background.thanks

Dimezis commented 3 years ago

Please be more specific, I'm not going to write all the code for you. Tell me what you tried in details and what's not working.

namnguyen211 commented 3 years ago

class MyClass :BottomSheetDialogFragment(){

private var _binding: MyClassViewBinding? = null
private val binding get() = _binding!!

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = MyClassViewBinding.inflate(inflater, container, false)
        val blurContainer = BlurView(requireContext())
        blurContainer.id = BLUR_ID
        blurContainer.addView(binding.root)
        return blurContainer
  }

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val blurView = view.findViewById<BlurView>(BLUR_ID)
    blurView.setupWith(requireActivity().window.decorView as ViewGroup)
        .setFrameClearDrawable(requireActivity().window.decorView.background)
        .setBlurAlgorithm(RenderScriptBlur(requireContext()))
        .setBlurRadius(10f)
}

}

this is my code. background not blur.

Dimezis commented 3 years ago

Your BlurView doesn't have a size: val blurContainer = BlurView(requireContext())

Set proper LayoutParams or just put it in your XML

namnguyen211 commented 3 years ago
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentSinglePhotoViewBinding.inflate(inflater, container, false)
    val blurContainer = BlurView(requireContext())
    blurContainer.id = BLUR_ID
    val param = ViewGroup.LayoutParams(
        /*width*/
        ViewGroup.LayoutParams.MATCH_PARENT,  /*height*/
        ViewGroup.LayoutParams.MATCH_PARENT,
    )
    blurContainer.layoutParams = param
    blurContainer.addView(binding.root)
    return blurContainer
}

i added LayoutParams but not works.

Dimezis commented 3 years ago

You also need to set .setBlurAutoUpdate(true) as in Readme

namnguyen211 commented 3 years ago
   val blurView = view.findViewById<BlurView>(BLUR_ID)
    blurView.setupWith(requireActivity().window.decorView as ViewGroup)
        .setFrameClearDrawable(requireActivity().window.decorView.background)
        .setBlurAlgorithm(RenderScriptBlur(requireContext()))
        .setHasFixedTransformationMatrix(true)
        .setBlurAutoUpdate(true)
        .setBlurRadius(20f)

it still not works

Dimezis commented 3 years ago

setHasFixedTransformationMatrix(false) and post your XML layout. Make sure it doesn't have a background that would cover blur or anything like that

namnguyen211 commented 3 years ago

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:background="@color/black" /> </RelativeLayout>

Screenshot_20211001-112507

namnguyen211 commented 3 years ago

`package com.example.bottomsheetdialog

import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.google.android.material.bottomsheet.BottomSheetDialogFragment import eightbitlab.com.blurview.BlurView import eightbitlab.com.blurview.RenderScriptBlur

class BlurBottomSheetDialogFragment : BottomSheetDialogFragment() {

private val BLUR_ID = 514214;

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    val blurContainer = BlurView(requireContext())
    blurContainer.id = BLUR_ID
    blurContainer.layoutParams = ViewGroup.LayoutParams(
        /*width*/
        ViewGroup.LayoutParams.MATCH_PARENT,  /*height*/
        ViewGroup.LayoutParams.MATCH_PARENT,
    )
    blurContainer.addView(inflater.inflate(R.layout.dialog_connect_wifi, container, false))
    return blurContainer
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // this part is important to make injected background blur
    val blurView = view.findViewById<BlurView>(BLUR_ID)
    blurView.setupWith(requireActivity().window.decorView as ViewGroup)
        .setFrameClearDrawable(requireActivity().window.decorView.background)
        .setBlurAlgorithm(RenderScriptBlur(requireContext()))
        .setHasFixedTransformationMatrix(false)
        .setBlurAutoUpdate(true)
        .setBlurRadius(10f)
}

}`

this is my file bottomsheet dialog

Dimezis commented 3 years ago

But what do you want to blur?

You have plain white background. Blurring it will produce plain white color, as you have on your dialog.

Try adding at least something to the bottom of your activity to see the blur.