Skip to content

Commit

Permalink
Safe checks for RenderEffectBlur and a deprecation note
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimezis committed Jun 14, 2022
1 parent 9d7a593 commit 2af48f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.eightbitlab.blurview_sample;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.SeekBar;
Expand All @@ -14,10 +13,7 @@

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;

public class MainActivity extends AppCompatActivity {
Expand Down Expand Up @@ -61,15 +57,9 @@ private void setupBlurView() {
//set background, if your root layout doesn't have one
final Drawable windowBackground = getWindow().getDecorView().getBackground();

BlurAlgorithm algorithm;
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
algorithm = new RenderEffectBlur(topBlurView, RenderEffectPrecision.EXACT);
} else {
algorithm = new RenderScriptBlur(this);
}
topBlurView.setupWith(root)
.setFrameClearDrawable(windowBackground)
.setBlurAlgorithm(algorithm)
.setBlurAlgorithm(new RenderScriptBlur(this))
.setBlurRadius(radius);

bottomBlurView.setupWith(root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
* Hardware acceleration is supported.
* Its performance and stability is not yet well studied, use at own risk.
* There's a known downside - this BlurAlgorithm constantly triggers a redraw of the BlurView.
*
* @deprecated not exactly deprecated, but this algorithm shouldn't be used, because it causes a constant redraw of the BlurView,
* and doesn't seem to provide a significant benefit over RenderScriptBlur.
*/
@RequiresApi(Build.VERSION_CODES.S)
@Deprecated
public class RenderEffectBlur implements BlurAlgorithm {

/**
Expand All @@ -35,20 +39,29 @@ public RenderEffectBlur(BlurView blurView, RenderEffectPrecision precision) {
backgroundView = new View(blurView.getContext());
this.precision = precision;

blurView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
blurView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
blurView.getMeasuredHeight()
);
if (backgroundView.getParent() != null) {
((ViewGroup)backgroundView.getParent()).removeView(backgroundView);
if (blurView.isInLayout() || !blurView.isLaidOut()) {
blurView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
blurView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
addBackground(blurView);
}
blurView.addView(backgroundView, 0, params);
}
});
});
} else {
addBackground(blurView);
}
}

private void addBackground(BlurView blurView) {
ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
blurView.getMeasuredHeight()
);
// https://github.com/Dimezis/BlurView/pull/180
// Guard against some android quirk
if (backgroundView.getParent() == null) {
blurView.addView(backgroundView, 0, params);
}
}

@Override
Expand Down

0 comments on commit 2af48f1

Please sign in to comment.