Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

Android BlurMaskFilter has no effect in canvas.drawOval while text is blurred

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.

The Oval is not blurred yet the text is blurred.

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);
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...