我有一个我已经透明的Linearlayout,现在我正在寻找一种方法来赋予它一个模糊效果,所以它下面的东西变得模糊。就像Windows 7 Aero外观一样(见截图)。
我知道你可以做这样的模糊效果:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
但这仅适用于在出现对话框时模糊背景。
我一直在谷歌搜索近一个小时,我找不到任何东西。有没有人对如何做到这一点有任何建议?
由于
答案 0 :(得分:48)
在Android上实时模糊仍然是一个障碍。这是一些可行机制之间的综合比较:
StackBlur (已在
Onur
下的fastBlur
下面的答案中列出:
外观(半径20):
生成每个BitmapDrawable
的记录时间(毫秒):
I/(10266): Total time taken: 35
I/(10266): Total time taken: 54
I/(10266): Total time taken: 48
I/(10266): Total time taken: 36
I/(10266): Total time taken: 48
I/(10266): Total time taken: 39
I/(10266): Total time taken: 49
I/(10266): Total time taken: 50
I/(10266): Total time taken: 35
I/(10266): Total time taken: 47
平均值=> ~44.1 ms =>每秒22个drawables
<强>的renderScript 强>:
ScriptIntrinsicBlur
提供持续快速模糊。使用支持库可以使用api 8。
它的样子(在半径20处):
生成每个BitmapDrawable
的记录时间(毫秒):
I/(9342): Total time taken: 14
I/(9342): Total time taken: 16
I/(9342): Total time taken: 13
I/(9342): Total time taken: 28
I/(9342): Total time taken: 14
I/(9342): Total time taken: 12
I/(9342): Total time taken: 14
I/(9342): Total time taken: 19
I/(9342): Total time taken: 13
I/(9342): Total time taken: 13
平均值=&gt; ~15.6 ms =&gt;每秒64个drawables
RenderScript(半径= 3)+缩放(20%):
这是获得体面(?)但快速模糊的另一种方式。我们所做的是将位图缩放到其大小的一小部分 - 比如20% - 并在缩小版本上应用模糊算法。完成后,我们将位图缩放到原始大小。结果不如在原始图像上使用模糊算法那么好,但它们是可以通过的。另请注意,半径值不应太高,否则生成的位图将难以辨别。
它看起来像什么:
生成每个BitmapDrawable
的记录时间(毫秒):
I/(11631): Total time taken: 5
I/(11631): Total time taken: 19
I/(11631): Total time taken: 3
I/(11631): Total time taken: 7
I/(11631): Total time taken: 7
I/(11631): Total time taken: 5
I/(11631): Total time taken: 7
I/(11631): Total time taken: 17
I/(11631): Total time taken: 5
I/(11631): Total time taken: 4
平均值=&gt; ~7.9 ms =&gt;每秒126个drawables
StackBlur(半径= 3)+缩放(20%):
与上面#3相同的概念。将大小缩小到20%并在缩放的位图上应用stackblur。
I/(12690): Total time taken: 4
I/(12690): Total time taken: 20
I/(12690): Total time taken: 4
I/(12690): Total time taken: 4
I/(12690): Total time taken: 5
I/(12690): Total time taken: 5
I/(12690): Total time taken: 4
I/(12690): Total time taken: 21
I/(12690): Total time taken: 3
I/(12690): Total time taken: 4
平均值=&gt; ~7.7 ms =&gt;每秒135个drawables
在Nex4上进行的测试。 位图大小 - 200px x 200px
另一个提示:方法buildDrawingCache()
和getDrawingCache()
本身需要很长时间。另一种方法是使用模糊所需的视图尺寸创建一个位图:
Bitmap toDrawOn = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
// Create a canvas - assign `toDrawOn` as an offscreen bitmap to it
Canvas holdingCanvas = new Canvas(toDrawOn);
// Now, let the view draw itself on this canvas
yourView.draw(holdingCanvas);
视图现在在toDrawOn
上绘制,您可以随意使用它。
根据我的经验,这比生成和访问视图的绘图缓存快得多。
如果您需要帮助实施上面列出的4种方法中的任何一种,请在评论中告诉我。
请记住,上面的GIF已缩小尺寸等等。如果您想查看原始屏幕截图(mp4)文件,请检查此Link。
答案 1 :(得分:30)
这在我的脑海中已有一段时间了,我刚刚通过你的问题实现了它。
为了能够做到这一点,我们需要将模糊布局下面的布局绘制到位图中。与使用模糊算法相比,我们需要模糊该位图,最后绘制模糊位图作为模糊布局的背景。
幸运的是android已经缓存了绘图机制,所以第一部分很容易。我们可以简单地为我们的底层布局启用缓存绘图,并使用getDrawingCache()
从中获取位图。
现在我们需要快速模糊算法。我使用了https://stackoverflow.com/a/10028267/3133545
在这里。
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import java.lang.ref.WeakReference;
import java.util.InputMismatchException;
/**
* A drawable that draws the target view as blurred using fast blur
* <p/>
* <p/>
* TODO:we might use setBounds() to draw only part a of the target view
* <p/>
* Created by 10uR on 24.5.2014.
*/
public class BlurDrawable extends Drawable {
private WeakReference<View> targetRef;
private Bitmap blurred;
private Paint paint;
private int radius;
public BlurDrawable(View target) {
this(target, 10);
}
public BlurDrawable(View target, int radius) {
this.targetRef = new WeakReference<View>(target);
setRadius(radius);
target.setDrawingCacheEnabled(true);
target.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
}
@Override
public void draw(Canvas canvas) {
if (blurred == null) {
View target = targetRef.get();
if (target != null) {
Bitmap bitmap = target.getDrawingCache(true);
if (bitmap == null) return;
blurred = fastBlur(bitmap, radius);
}
}
if (blurred != null && !blurred.isRecycled())
canvas.drawBitmap(blurred, 0, 0, paint);
}
/**
* Set the bluring radius that will be applied to target view's bitmap
*
* @param radius should be 0-100
*/
public void setRadius(int radius) {
if (radius < 0 || radius > 100)
throw new InputMismatchException("Radius must be 0 <= radius <= 100 !");
this.radius = radius;
if (blurred != null) {
blurred.recycle();
blurred = null;
}
invalidateSelf();
}
public int getRadius() {
return radius;
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
/**
* from https://stackoverflow.com/a/10028267/3133545
* <p/>
* <p/>
* <p/>
* Stack Blur v1.0 from
* http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
* <p/>
* Java Author: Mario Klingemann <mario at quasimondo.com>
* http://incubator.quasimondo.com
* created Feburary 29, 2004
* Android port : Yahel Bouaziz <yahel at kayenko.com>
* http://www.kayenko.com
* ported april 5th, 2012
* <p/>
* This is a compromise between Gaussian Blur and Box blur
* It creates much better looking blurs than Box Blur, but is
* 7x faster than my Gaussian Blur implementation.
* <p/>
* I called it Stack Blur because this describes best how this
* filter works internally: it creates a kind of moving stack
* of colors whilst scanning through the image. Thereby it
* just has to add one new block of color to the right side
* of the stack and remove the leftmost color. The remaining
* colors on the topmost layer of the stack are either added on
* or reduced by one, depending on if they are on the right or
* on the left side of the stack.
* <p/>
* If you are using this algorithm in your code please add
* the following line:
* <p/>
* Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
*/
private static Bitmap fastBlur(Bitmap sentBitmap, int radius) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
if (radius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
Log.e("pix", w + " " + h + " " + pix.length);
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;
int r[] = new int[wh];
int g[] = new int[wh];
int b[] = new int[wh];
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
int vmin[] = new int[Math.max(w, h)];
int divsum = (div + 1) >> 1;
divsum *= divsum;
int dv[] = new int[256 * divsum];
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (i / divsum);
}
yw = yi = 0;
int[][] stack = new int[div][3];
int stackpointer;
int stackstart;
int[] sir;
int rbs;
int r1 = radius + 1;
int routsum, goutsum, boutsum;
int rinsum, ginsum, binsum;
for (y = 0; y < h; y++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
for (i = -radius; i <= radius; i++) {
p = pix[yi + Math.min(wm, Math.max(i, 0))];
sir = stack[i + radius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rbs = r1 - Math.abs(i);
rsum += sir[0] * rbs;
gsum += sir[1] * rbs;
bsum += sir[2] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
}
stackpointer = radius;
for (x = 0; x < w; x++) {
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (y == 0) {
vmin[x] = Math.min(x + radius + 1, wm);
}
p = pix[yw + vmin[x]];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[(stackpointer) % div];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
yp = -radius * w;
for (i = -radius; i <= radius; i++) {
yi = Math.max(0, yp) + x;
sir = stack[i + radius];
sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];
rbs = r1 - Math.abs(i);
rsum += r[yi] * rbs;
gsum += g[yi] * rbs;
bsum += b[yi] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
if (i < hm) {
yp += w;
}
}
yi = x;
stackpointer = radius;
for (y = 0; y < h; y++) {
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (x == 0) {
vmin[y] = Math.min(y + r1, hm) * w;
}
p = x + vmin[y];
sir[0] = r[p];
sir[1] = g[p];
sir[2] = b[p];
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[stackpointer];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi += w;
}
}
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return (bitmap);
}
}
用法:
View beneathView = //the view that beneath blur view
View blurView= //blur View
BlurDrawable blurDrawable = new BlurDrawable(beneathView, radius);
blurView.setBackgroundDrawable(blurDrawable);
我的测试应用程序如何显示:
我决定不使用这个,因为它太过于hacky而且看起来并不像我认为的那样酷。
答案 2 :(得分:12)
Here是GDE Mario Viviani“有效使用Renderscript模糊图像”的好方法。
这是复制/粘贴:
模糊图像是很多开发人员需要实现的效果,可能需要一些时间和精力才能实现。此外,由于需要进行大量的图像处理,如果没有进行适当的编码,就CPU和内存使用而言,这真的很麻烦。
有一种快速有效的模糊图像解决方案,即Renderscript。
自API 11(Honeycomb
)以来可用,Renderscript
允许利用GPU加速,并且针对高性能3D渲染和计算操作。
Renderscript
是一个非常复杂和清晰的产品,允许使用本机C99语言进行深度配置和编码,从而实现可移植性,性能和可用性。
但是,由于API 17(4.2.2)Renderscript
提供了一些执行定义明确的操作的内置函数,称为内在函数。
内在函数是预先编写的脚本,允许执行Blur,Blen,Matrix Convolution等操作,而无需编写Renderscript
代码。
这是我编写的一个简单方法,可以轻松地有效地将模糊滤镜应用于位图:
public Bitmap blurBitmap(Bitmap bitmap) {
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(getApplicationContext());
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the in/out Allocations with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
和...瞧!模糊的位图! : - )
请记住,要运行上一代码,您需要最低限度的API 17(4.2.2)。
以下是此方法的要点: https://gist.github.com/Mariuxtheone/903c35b4927c0df18cf8
如果您想了解有关Intrinsics的更多信息,请查看Android开发者博客上的这篇文章: http://android-developers.blogspot.it/2013/08/renderscript-intrinsics.html
如果您有兴趣了解有关Renderscript的更多信息,请查看以下链接: http://android-developers.blogspot.it/2011/02/introducing-renderscript.html http://android-developers.blogspot.it/2011/03/renderscript.html
答案 3 :(得分:0)
使用布局功能无法轻松完成。
我建议将父视图绘制到画布,如下所述:How to make any view to draw to canvas?隐藏前景视图。然后,模糊画布并使用此数据作为背景或其中的一部分绘制前景视图。
如果您希望此效果生效,那将是非常耗费资源的操作。你应该至少尝试缓存模糊结果。
答案 4 :(得分:0)
尝试使用这个库我认为你只需要传递它的视图就可以了......
答案 5 :(得分:0)
步骤1.在位图中剪切背景图像的一部分。
第2步。模糊位图的那部分。
步骤3.将位图设置为背景。
private void applyBlur(final View image, final View layout) {
image.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
image.getViewTreeObserver().removeOnPreDrawListener(this);
image.buildDrawingCache();
Bitmap bmp = image.getDrawingCache();
Bitmap overlay = Bitmap.createBitmap((int) (layout.getMeasuredWidth()),
(int) (layout.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.translate(-layout.getLeft(), -layout.getTop());
canvas.drawBitmap(bmp, 0, 0, null);
RenderScript rs = RenderScript.create(getActivity());
Allocation overlayAlloc = Allocation.createFromBitmap(
rs, overlay);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(20);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(overlay);
layout.setBackground(new BitmapDrawable(
getResources(), overlay));
rs.destroy();
return true;
}
});
}
现在调用此函数:
applyBlur(detail_main_image,titleLayout);
//其中detail_main_image是我必须显示模糊部分的图像 //和titleLayout是我必须设置背景模糊的视图。