wcoder / Xamarin.BlurView

Dynamic iOS-like blur of underlying Views for Android.
https://github.com/Dimezis/BlurView
Apache License 2.0
28 stars 2 forks source link

BlurView in custom renderer #1

Closed leeapted closed 6 years ago

leeapted commented 6 years ago

I'm trying to use BlurView in a custom renderer. When using a FrameLayout, the following code works as expected:

    protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e) {
        base.OnElementChanged(e);

        var BlurView0=new FrameLayout(Context0);
        BlurView0.SetBackgroundColor(Android.Graphics.Color.Aqua);
        BlurView0.Layout(10, 20, 500, 200); 
        this.AddView(BlurView0);
    }

But I cannot get the following to work when I replace the FrameLayout with a BlurView (there is no blurring). Is it possible? What is Wrong? Thank you!

    protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e) {
        base.OnElementChanged(e);

        var BlurView0=new BlurView(Context0);

        var activity = Context0 as Activity;

        ViewGroup RootView = (ViewGroup) activity.Window.DecorView.FindViewById(Android.Resource.Id.Content);

        Drawable windowBackground = activity.Window.DecorView.Background;

        BlurView0.SetupWith(RootView)
            .WindowBackground(windowBackground)
            .BlurAlgorithm(new RenderScriptBlur(Context0))
            .BlurRadius(20f);

        BlurView0.Layout(10, 20, 500, 200); 

        this.AddView(BlurView0);
    }
wcoder commented 6 years ago

Hi @leeapted

You need to set overlay color for BlurView:

blurView.SetOverlayColor(Resource.Color.colorOverlay);

Android colors.xml

<color name="colorOverlay">#78ffffff</color>

I added full worked Xamarin.Forms example here: https://github.com/wcoder/Xamarin.BlurView/pull/2/commits/50c71e4bcc58cc07deb108c18dd6a40ccd077832

leeapted commented 6 years ago

It works perfectly. I'm using it in a titlebar on top of a Xamarin ListView (scrolling underneath it) and it's very smooth. Thank you.