diff --git a/app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java b/app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java index 6eaa5e6..6dbfd44 100644 --- a/app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java +++ b/app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java @@ -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; @@ -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 { @@ -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) diff --git a/library/src/main/java/eightbitlab/com/blurview/RenderEffectBlur.java b/library/src/main/java/eightbitlab/com/blurview/RenderEffectBlur.java index d42fb50..eb15daf 100644 --- a/library/src/main/java/eightbitlab/com/blurview/RenderEffectBlur.java +++ b/library/src/main/java/eightbitlab/com/blurview/RenderEffectBlur.java @@ -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 { /** @@ -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