我一直在尝试创建一个自定义视图,该视图在文本下具有模糊的形状。问题是 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));
我在 onDraw() 中调用这样的函数:
canvas.drawOval(mShadowBounds,mShadowPaint);
canvas.drawText("hello", x, y, paint);
但这就是我所看到的。
使用 android 4.0 sdk 并在 4.0.4 Galaxy nexus 设备(英国)上进行测试。我想知道这是否是 4.0.4 中的错误,因为我确实在 4.0 和 4.0.3 的模拟器上对其进行了测试,并且它在它们上的模糊效果非常好,除非我做错了什么?
编辑:这是在其他平台上测试它的扩展视图代码。
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);
}
}