问题描述
我一直在试图创建一个已根据文字模糊的图形自定义视图。的问题是,BlurMaskFilter对我在画布上绘制任何形状没有影响。下面是我如何初始化构造函数中的油漆对象:
颜料=新的油漆(0);
paint.setColor(为0xffffffff);
paint.setMaskFilter(新BlurMaskFilter(8,BlurMaskFilter.Blur.NORMAL));
mShadowPaint =新的油漆(0);
mShadowPaint.setColor(0xff333333);
mShadowPaint.setMaskFilter(新BlurMaskFilter(10,BlurMaskFilter.Blur.NORMAL));
和我打电话像这样的的OnDraw()函数:
canvas.drawOval(mShadowBounds,mShadowPaint);
canvas.drawText(你好,X,Y,油漆);
但是,这是我所看到的。
一4.0.4 Galaxy Nexus的设备(英国)上采用了Android 4.0 SDK和测试。我不知道如果这是在4.0.4中的错误,因为我没有在模拟器上与4.0和4.0.3进行测试,它也模糊得很清楚他们,除非我做一些完全错误的?
编辑:这里是扩展查看code到其他平台上测试它
进口android.content.Context;
进口android.graphics.BlurMaskFilter;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.RectF;
进口android.util.AttributeSet;
进口android.view.View;
公共类BlurTestView扩展视图{
民营涂料粉刷;
私人油漆mShadowPaint;
私人诠释大小= 100;
私人RectF mShadowBounds =新RectF();
公共BlurTestView(上下文的背景下){
这(背景下,NULL,0);
}
公共BlurTestView(上下文的背景下,ATTRS的AttributeSet){
这(背景下,ATTRS,0);
}
公共BlurTestView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
油漆=新的油漆(0);
paint.setColor(0xff333333);
paint.setTextSize(大小);
paint.setMaskFilter(新BlurMaskFilter(8,BlurMaskFilter.Blur.NORMAL));
mShadowPaint =新的油漆(0);
mShadowPaint.setColor(0xff333333);
mShadowPaint.setMaskFilter(新BlurMaskFilter(10,BlurMaskFilter.Blur.NORMAL));
mShadowBounds.top =大小;
mShadowBounds.bottom = mShadowBounds.top +(尺寸/ 2);
mShadowBounds.left = 0;
mShadowBounds.right =(INT)paint.measureText(你好);
}
@覆盖
公共无效的OnDraw(帆布油画)
{
canvas.drawOval(mShadowBounds,mShadowPaint);
canvas.drawText(你好,0,大小,油漆);
}
}
看起来像我的错误。我报给Android团队;我们将看到他们在说什么。
hardwareAccelerated =假您在的AndroidManifest.xml
活动:如果您设置安卓它使得正确。
下面是从Android图形球队官方消息:BlurMaskFilter不支持硬件加速。(截至2012年7月10号的)
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:
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():
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?
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);
}
}
Looks like a bug to me. I reported it to the Android team; we'll see what they say.
It renders correctly if you set android:hardwareAccelerated="false"
on your Activity in 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在canvas.drawOval没有效果,而文字模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!