Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: wrong activity background blurring #179

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.eightbitlab.blurview_sample;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.*;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import eightbitlab.com.blurview.BlurAlgorithm;
import eightbitlab.com.blurview.BlurView;
import eightbitlab.com.blurview.RenderEffectBlur;
import eightbitlab.com.blurview.RenderEffectPrecision;
import eightbitlab.com.blurview.RenderScriptBlur;
import eightbitlab.com.blurview.*;

public class MainActivity extends AppCompatActivity {

@@ -54,12 +48,12 @@ private void setupViewPager() {
}

private void setupBlurView() {
final float radius = 25f;
final float radius = 5f;
final float minBlurRadius = 10f;
final float step = 4f;

//set background, if your root layout doesn't have one
final Drawable windowBackground = getWindow().getDecorView().getBackground();
final View windowBackground = getWindow().getDecorView();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not what I meant :)
Please set it up like this. No need to change library's API

ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
topBlurView.setupWith(decorView)
        .setBlurAlgorithm(algorithm)
        .setBlurRadius(radius);

bottomBlurView.setupWith(decorView)
        .setBlurAlgorithm(new RenderScriptBlur(this))
        .setBlurRadius(radius);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dimezis From your comment on setupWith

The lower amount of Views are in the root, the better for performance.

I agree with this point, so changing root view is not the thing I'm trying to do with the fix. I'm still would like to add a support for the lib's API to support getting decor background by getWindow().getDecorView() for blurring. This approach brings better performance, right?

Again, I think this logic could help https://github.com/mmin18/RealtimeBlurView/blob/master/library/src/com/github/mmin18/widget/RealtimeBlurView.java#L245-L261 🤔

Copy link
Owner

@Dimezis Dimezis May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think this logic could help

If you squint your eyes, you'll see that's exactly what that library does - it draws the whole DecorView on the Canvas, and that's exactly why it works.

I'm still would like to add a support for the lib's API to support getting decor background by getWindow().getDecorView() for blurring

I'm not sure what you're trying to achieve in this PR, the bug is still there and this code essentially works the same as before - it takes the background from the DecorView. But instead of passing it directly, you pass the DecorView and take its background later.

This approach brings better performance, right?

The code in this PR performs identically to the original code.

If you're asking about the performance of drawing the whole DecorView when it's passed as a root, it is indeed slightly worse in theory, but I don't think you'll see a measurable difference.


BlurAlgorithm algorithm;
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
@@ -131,6 +125,12 @@ Fragment getFragment() {
Fragment getFragment() {
return new ImageFragment();
}
},
FOURTH("Transparent") {
@Override
Fragment getFragment() {
return new TransparentFragment();
}
};

private String title;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.eightbitlab.blurview_sample;

public class TransparentFragment extends BaseFragment {
@Override
int getLayoutId() {
return R.layout.fragment_transparent;
}
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_image.xml
Original file line number Diff line number Diff line change
@@ -3,5 +3,6 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:adjustViewBounds="true"
android:src="@drawable/trees"/>
android:src="@drawable/trees"/>
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_list.xml
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clipToPadding="false"
android:paddingBottom="@dimen/bottom_tab_height">

</androidx.recyclerview.widget.RecyclerView>
</androidx.recyclerview.widget.RecyclerView>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_scroll.xml
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:paddingBottom="@dimen/bottom_tab_height"
android:clipToPadding="false"
android:scrollbars="none">
4 changes: 4 additions & 0 deletions app/src/main/res/layout/fragment_transparent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -5,5 +5,6 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@drawable/lines</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package eightbitlab.com.blurview;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.graphics.*;
import android.view.*;

import androidx.annotation.*;

/**
* Blur Controller that handles all blur logic for the attached View.
@@ -59,7 +52,7 @@ public boolean onPreDraw() {
private boolean initialized;

@Nullable
private Drawable frameClearDrawable;
private View frameClearDrawable;
private final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);

/**
@@ -105,7 +98,7 @@ void updateBlur() {
if (frameClearDrawable == null) {
internalBitmap.eraseColor(Color.TRANSPARENT);
} else {
frameClearDrawable.draw(internalCanvas);
frameClearDrawable.getBackground().draw(internalCanvas);
}

internalCanvas.save();
@@ -205,7 +198,7 @@ public BlurViewFacade setBlurAlgorithm(BlurAlgorithm algorithm) {
}

@Override
public BlurViewFacade setFrameClearDrawable(@Nullable Drawable frameClearDrawable) {
public BlurViewFacade setFrameClearDrawable(@Nullable View frameClearDrawable) {
this.frameClearDrawable = frameClearDrawable;
return this;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eightbitlab.com.blurview;

import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

@@ -41,7 +42,7 @@ public interface BlurViewFacade {
* Optional, by default frame is cleared with a transparent color.
* @return {@link BlurViewFacade}
*/
BlurViewFacade setFrameClearDrawable(@Nullable Drawable frameClearDrawable);
BlurViewFacade setFrameClearDrawable(@Nullable View frameClearDrawable);

/**
* @param radius sets the blur radius
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package eightbitlab.com.blurview;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.Nullable;

//Used in edit mode and in case if no BlurController was set
@@ -35,7 +36,7 @@ public BlurViewFacade setOverlayColor(int overlayColor) {
}

@Override
public BlurViewFacade setFrameClearDrawable(@Nullable Drawable windowBackground) {
public BlurViewFacade setFrameClearDrawable(@Nullable View windowBackground) {
return this;
}