BlurView

repository·master·Indexed 26 days ago

https://github.com/dimezis/blurview

An Android library that provides dynamic, iOS-like blur effects for Views by rendering a snapshot of a target view hierarchy as a blurred background. It supports custom blur radii, scale factors for performance optimization, and OpenGL-based blurring for API 29-30. The library requires wrapping content in a BlurTarget and linking it via the setupWith() method.

Tokens
1.2K
Snippets
3
Records
9
Agent score
88%

What's inside BlurView

  1. Implement BlurView in XML layout

    master

    To use BlurView, you must wrap the content you want to blur inside an eightbitlab.com.blurview.BlurTarget. The BlurView is then placed as a separate view (typically an overlay) that will render the blurred snapshot of the target as its background. Children of the BlurView will not be blurred.

    <!-- This is the content to be blurred by the BlurView. -->
    <eightbitlab.com.blurview.BlurTarget
        android:id="@+id/target"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <!-- Your main content here -->
    
    </eightbitlab.com.blurview.BlurTarget>
    
    <eightbitlab.com.blurview.BlurView
      android:id="@+id/blurView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:blurOverlayColor="@color/colorOverlay">
    
       <!-- Any child View here (e.g., TabLayout) will NOT be blurred -->
    
    </eightbitlab.com.blurview.BlurView>
  2. Handle Animations in BlurView 3.0

    master

    While BlurView automatically honors position, scale, and rotation transformations, certain types of animations require manual notification to ensure the blur renders correctly.

    • Automatic: Animating translationX, translationY, setScaleX, or setRotation works without extra steps.
    • Manual: If using an Animator (e.g., blurView.animate().rotation(...)), you must attach an update listener and call blurView.notifyRotationChanged(...) on every update.

    Note: animation.getAnimatedValue() returns a fraction from 0 to 1, so you must multiply it by your target angle.

    int angle = 90;
    blurView.animate().rotation(angle).setUpdateListener(animation -> {
        // getAnimatedValue really returns just a fraction from 0 to 1
        blurView.notifyRotationChanged((Float) animation.getAnimatedValue() * angle);        
    });
  3. Migrate to BlurView 3.0

    master

    BlurView 3.0 introduces a significant API change. Instead of the old direct setup, you must now wrap the content you want to blur into a BlurTarget and pass that target into the BlurView.setupWith() method.

    Key constraints:

    • A BlurTarget may not contain a BlurView that targets that same BlurTarget (to avoid infinite loops).
    • A BlurTarget can contain other BlurTargets and BlurViews.
  4. Configure Scale Factor in BlurView 3.0

    master

    The scale factor reduces the size of the View snapshot to improve blur performance at the cost of quality.

    • Default scale factor: 4 (previously 6 in older versions).
    • API 31+ behavior: RenderEffect internally scales the snapshot. For example, a blur radius of 20 with a scale factor of 3 is equivalent to a blur radius of 60 with a scale factor of 1.
    • API < 31 behavior: The scale factor is critical for maintaining reasonable performance.

    You can control the scale factor by passing it as an argument to the setupWith() method.

  5. BlurView compatibility with SurfaceView and TextureView

    master

    Note the following limitations regarding view types:

    • TextureView: Can be blurred only on API 31+.
    • SurfaceView-based views (including VideoView, MapFragment, GLSurfaceView, etc.): Cannot be blurred.
  6. Use OpenGLBlurAlgorithm for API 29-30

    master

    The OpenGLBlurAlgorithm is a BlurAlgorithm implementation designed for Android API levels 29 (Q) and 30. It uses OpenGL to blur a snapshot into a HardwareBuffer-backed bitmap.

    Key characteristics:

    • Thread Safety: All work runs on the UI thread to ensure the blur does not trail the content.
    • Limitations: Like RenderScript, it may not render TextureView content or hardware-backed bitmaps.
    • Supported Config: It only supports Bitmap.Config.ARGB_8888.