Android BlurMaskFilter 在canvas.drawOval 中没有效果,而文本是模糊的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11281404/
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
Android BlurMaskFilter has no effect in canvas.drawOval while text is blurred
提问by fasih.rana
I've been trying to create a custom view which has blurred shapes under text. The problem is that the BlurMaskFilter has no effect on any shape that I draw on the canvas. Here is how I'm initialising the Paint objects in the constructor:
我一直在尝试创建一个自定义视图,该视图在文本下具有模糊的形状。问题是 BlurMaskFilter 对我在画布上绘制的任何形状都没有影响。这是我在构造函数中初始化 Paint 对象的方式:
paint = new Paint(0);
paint.setColor(0xffffffff);
paint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));
mShadowPaint = new Paint(0);
mShadowPaint.setColor(0xff333333);
mShadowPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
And I'm calling the functions like this in onDraw():
我在 onDraw() 中调用这样的函数:
canvas.drawOval(mShadowBounds,mShadowPaint);
canvas.drawText("hello", x, y, paint);
But this is what I see.
但这就是我所看到的。
Using android 4.0 sdk and testing on a 4.0.4 galaxy nexus device (UK). I'm wondering if this is a bug in 4.0.4 as I did test it on the emulator with 4.0 and 4.0.3 and it did blur perfectly well on them, unless I'm doing something completely wrong?
使用 android 4.0 sdk 并在 4.0.4 Galaxy nexus 设备(英国)上进行测试。我想知道这是否是 4.0.4 中的错误,因为我确实在 4.0 和 4.0.3 的模拟器上对其进行了测试,并且在它们上确实模糊得很好,除非我做错了什么?
EDIT: Here is the extended View code to test it on other platforms.
编辑:这是在其他平台上测试它的扩展视图代码。
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class BlurTestView extends View{
private Paint paint;
private Paint mShadowPaint;
private int size = 100;
private RectF mShadowBounds = new RectF();
public BlurTestView(Context context) {
this(context, null, 0);
}
public BlurTestView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BlurTestView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint(0);
paint.setColor(0xff333333);
paint.setTextSize(size);
paint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));
mShadowPaint = new Paint(0);
mShadowPaint.setColor(0xff333333);
mShadowPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
mShadowBounds.top = size;
mShadowBounds.bottom = mShadowBounds.top+(size /2);
mShadowBounds.left = 0;
mShadowBounds.right = (int)paint.measureText("hello");
}
@Override
public void onDraw(Canvas canvas)
{
canvas.drawOval(mShadowBounds,mShadowPaint);
canvas.drawText("hello", 0, size, paint);
}
}
回答by Sparky
Looks like a bug to me. I reported it to the Android team; we'll see what they say.
对我来说看起来像一个错误。我向Android团队报告了;我们会看看他们怎么说。
It renders correctly if you set android:hardwareAccelerated="false"
on your Activity in AndroidManifest.xml
.
如果您android:hardwareAccelerated="false"
在AndroidManifest.xml
.
Here is the official word from the Android graphics team: "BlurMaskFilter is not supported with hardware acceleration." (As of July 10, 2012)
以下是 Android 图形团队的官方说法:“硬件加速不支持 BlurMaskFilter。” (截至 2012 年 7 月 10 日)
回答by Sergey Pekar
If you can not disable hardware acceleration in your activity (for example it uses TextureView which require hardware acceleration) you can just call setLayerTypewith first parameter LAYER_TYPE_SOFTWAREand the second parameter null for your view.
如果您无法在您的活动中禁用硬件加速(例如,它使用需要硬件加速的 TextureView),您只需使用第一个参数LAYER_TYPE_SOFTWARE和第二个参数 null 为您的视图调用setLayerType。
Like this
像这样
public class BlurTestView extends View {
public BlurTestView(Context context) {
this(context, null, 0);
}
public BlurTestView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BlurTestView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Disable hardware acceleration for this view
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
// Perform other initialisation
}
// Other methods and so on...
}
More info about mask filters, effects and shaders you can find here.
您可以在此处找到有关遮罩过滤器、效果和着色器的更多信息。
回答by a54studio
I had the same issue while adding a filter to a path. I noticed that setting the target to 13 or below allows the filters to work. 14 and up they didn't.
我在向路径添加过滤器时遇到了同样的问题。我注意到将目标设置为 13 或以下允许过滤器工作。14岁及以上他们没有。
android:targetSdkVersion="13"