Android 绘制模糊

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1979955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:03:22  来源:igfitidea点击:

Android draw with blur

androidcanvasdrawblur

提问by Tancho

I need do draw on Android's Canvas using Blur effect, it is a very simple feature, I need to draw a circular area, which is blurred (the foreground) and the background transparent, I can do everything with manipulating the colour alpha to do it with custom transparency but I need it to be blurred instead of transparent.. any ideas?

我需要使用模糊效果在 Android 的 Canvas 上进行绘制,这是一个非常简单的功能,我需要绘制一个圆形区域,该区域是模糊的(前景)和透明的背景,我可以通过操纵颜色 alpha 来完成所有操作具有自定义透明度,但我需要将其模糊而不是透明......有什么想法吗?

回答by user2903200

(For those who are coming back, though this is an old question)

(对于那些回来的人,虽然这是一个老问题)

On the Paint object which you are using to draw the Color, set

在用于绘制颜色的 Paint 对象上,设置

paint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL))

Then, in Android Manifest.xml, set

然后,在 Android Manifest.xml 中,设置

android:hardwareAccelerated="false"

for your activity (Blur doesn't work when hardware acceleration is true)

为您的活动(当硬件加速为真时模糊不起作用)

For more explanation refer Android BlurMaskFilter has no effect in canvas.drawOval while text is blurred

有关更多解释,请参阅 Android BlurMaskFilter 在 canvas.drawOval 中没有效果,而文本是模糊的

回答by Tancho

If you're not gonna need to manipulate it afterwards you could just draw to a buffer and blur it yourself pixel by pixel, then blit that buffer to screen when you need to paint it.

如果您之后不需要操作它,您可以只绘制到缓冲区并自己逐个像素地模糊它,然后在需要绘制它时将该缓冲区 blit 到屏幕上。

Or this: http://developer.android.com/reference/android/graphics/BlurMaskFilter.Blur.html

或者这个:http: //developer.android.com/reference/android/graphics/BlurMaskFilter.Blur.html

回答by Tobias Ritzau

Take a look at this: Android, Blur Bitmap instantly?

看看这个:Android,立即模糊位图?

Again I would recommend you first to draw into a bitmap. Scale it down (try different sizes), and the scale it back up to the original size (either as a new bitmap or if you use hw acceleration do it while you draw).

我再次建议您首先绘制位图。将其缩小(尝试不同的大小),然后将其缩放回原始大小(作为新的位图,或者如果您在绘制时使用硬件加速,请执行此操作)。