Skip to content

Commit

Permalink
Fix #110
Browse files Browse the repository at this point in the history
This addresses the issue when multiple BlurViews could trigger recursive redraw of each other
  • Loading branch information
Dimezis committed Feb 26, 2021
1 parent 093cd2c commit 1b371bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

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

/**
* Blur Controller that handles all blur logic for the attached View.
* It honors View size changes, View animation and Visibility changes.
Expand All @@ -32,7 +33,7 @@ final class BlockingBlurController implements BlurController {
private float blurRadius = DEFAULT_BLUR_RADIUS;

private BlurAlgorithm blurAlgorithm;
private Canvas internalCanvas;
private BlurViewCanvas internalCanvas;
private Bitmap internalBitmap;

@SuppressWarnings("WeakerAccess")
Expand Down Expand Up @@ -93,7 +94,7 @@ void init(int measuredWidth, int measuredHeight) {

blurView.setWillNotDraw(false);
allocateBitmap(measuredWidth, measuredHeight);
internalCanvas = new Canvas(internalBitmap);
internalCanvas = new BlurViewCanvas(internalBitmap);
initialized = true;

if (hasFixedTransformationMatrix) {
Expand Down Expand Up @@ -153,8 +154,10 @@ public boolean draw(Canvas canvas) {
if (!blurEnabled || !initialized) {
return true;
}
// Not blurring own children
if (canvas == internalCanvas) {
// Not blurring itself or other BlurViews to not cause recursive draw calls
// Related: https://github.com/Dimezis/BlurView/issues/110
// https://github.com/Dimezis/BlurView/issues/110
if (canvas instanceof BlurViewCanvas) {
return false;
}

Expand Down
14 changes: 14 additions & 0 deletions library/src/main/java/eightbitlab/com/blurview/BlurViewCanvas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eightbitlab.com.blurview;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import androidx.annotation.NonNull;

// Servers purely as a marker of a Canvas used in BlurView
// to skip drawing itself and other BlurViews on the View hierarchy snapshot
public class BlurViewCanvas extends Canvas {
public BlurViewCanvas(@NonNull Bitmap bitmap) {
super(bitmap);
}
}

0 comments on commit 1b371bf

Please sign in to comment.