android高斯模糊xml,Blur-LIB-Android

Blur-LIB-Android

A library for Blurring the background of a View.

PhoneAndScreen.gif

How it works

The blurring is really fast since everything is fully hardware accelerated. The background View that is to be blurred is rendered into a SurfaceTexture using Surface.lockHardwareCanvas().

The SurfaceTexture is then blurred using a Guassian blur algorithm and rendered in a SurfaceView or TextureView using OpenGL.

Download

Via Gradle

implementation 'no.danielzeller.blurbehindlib:blurbehindlib:1.0.0'

or Maven

no.danielzeller.blurbehindlib

blurbehindlib

1.0.0

pom

Basics

Create a BlurBehindLayout from XML

android:id="@+id/blurBehindLayout"

android:layout_width="match_parent"

android:layout_height="50dp"

app:blurRadius="100.0"

app:updateMode="onScroll"

app:useTextureView="false"

app:blurPaddingVertical="50dp"

app:blurTextureScale="0.5"

app:useChildAlphaAsMask="false">

Then you need to setup the View that is behind the BlurBehindLayout (the one that will be blurred).

blurBehindLayout.viewBehind = viewToBlur

Transition.gif

Blur radius

app:blurRadius = "100.0"

blurBehindLayout.blurRadius = 100.f

Determines how strong the blur is. Should be a float value between 0f-200f. Default is 40f.

Update mode

app:updateMode = "continuously"

blurBehindLayout.updateMode = UpdateMode.CONTINUOUSLY

Determines how the BlurBehindLayout is updated. When updateMode is UpdateMode.CONTINUOUSLY, the renderer is called repeatedly to re-render the scene.

When updateMode is UpdateMode.ON_SCROLL, the renderer only renders when a View is Scrolled.

When updateMode is UpdateMode.MANUALLY, the renderer only renders when the surface is created and when updateForMilliSeconds(..) is called manually. This is useful when animating the background or during a transition.

Use TextureView

app:useTextureView = "true"

This can only be changed in the constructor of the BlurBehindLayout either from xml or using the regular constructor from code. Default value is false. Using TextureView should only be used when SurfaceView is'nt an option, either because the Z-ordering breaks or if you animate the BlurBehindLayout's alpha value. Using TextureView instead of SurfaceView has a small impact on performance.

Blur texture scale

app:blurTextureScale = "0.5"

Should be a value between 0.1f-1f. It's recommended to downsample at least to 0.5f. The scale has a big impact on performance, so try keeping it as low as possible. Default is 0.4f. This can only be set in the constructor of the BlurBehindLayout either from xml or using the regular constructor from code.

Padding vertical

app:blurPaddingVertical = "50dp"

You can use this to make the Blur Texture larger than the BlurBehindLayout in the vertical direction. For instance when the background View is scrolled up and down it looks better with a padding, because it reduces flicker when new pixels enter the blurred area.

Use child as alpha mask

app:useChildAlphaAsMask = "true"

When this is true the first child View of the BlurBehindLayout is rendered into a texture. The alpha value of that texture is then used as mask for the Blur texture. When useChildAlphaAsMask is true, useTextureView will be forced to true as well in order to support transparency.

This effect can be used for creating text with blurred background and so on. See the DialogFragment for an example.

Contact

You can reach me on Twitter as @zellah or email.

Who's behind this?

Developed by Daniel Zeller - danielzeller.no, a freelance developer situated in Oslo, Norway.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,要实现高斯模糊遮罩可以使用一些现有的库或者自定义实现。以下是一种常用的方法: 1. 使用现有库: Android中有一些非常受欢迎的图像处理库,如Glide、Picasso等,它们都提供了对图像进行高斯模糊处理的功能。通常,只需将库添加到你的项目中,然后根据库的文档进行相应的调用即可。 2. 自定义实现: 如果你想自定义实现高斯模糊遮罩效果,你可以使用RenderScript。RenderScript是Android平台上的高性能计算框架,可以在运行时处理图像。下面是一个简单的实现示例: 首先,在你的项目中创建一个RenderScript文件: ``` rs文件:blur.rs #pragma version(1) #pragma rs java_package_name(com.example.blur) rs_allocation input; rs_allocation output; int radius; void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) { float4 sum = 0; int pixels = 0; const int2 center = {x, y}; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { int2 current = {center.x + i, center.y + j}; if (current.x >= 0 && current.y >= 0 && current.x < rsAllocationGetDimX(input) && current.y < rsAllocationGetDimY(input)) { float4 color = rsUnpackColor8888(rsGetElementAt_uchar4(input, current.x, current.y)); sum += color; pixels++; } } } float4 avgColor = sum / pixels; *v_out = rsPackColorTo8888(avgColor); } ``` 然后,在Java代码中调用RenderScript文件: ```java // 创建RenderScript实例 RenderScript rs = RenderScript.create(this); // 加载blur.rs文件 ScriptC_blur blurScript = new ScriptC_blur(rs); // 创建输入和输出的Allocation Allocation input = Allocation.createFromBitmap(rs, originalBitmap); Allocation output = Allocation.createTyped(rs, input.getType()); // 设置半径大小 blurScript.setRadius(10); // 设置输入输出Allocation blurScript.setInput(input); blurScript.setOutput(output); // 调用RenderScript进行高斯模糊处理 blurScript.forEach_root(input, output); // 将输出结果赋值给新的Bitmap Bitmap blurredBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), originalBitmap.getConfig()); output.copyTo(blurredBitmap); // 释放资源 input.destroy(); output.destroy(); blurScript.destroy(); rs.destroy(); ``` 使用这种方法,你可以根据自己的需求调整半径大小,从而实现不同程度的高斯模糊遮罩效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值