开发者

android: how to take a screenshot and blure it?

开发者 https://www.devze.com 2023-03-04 08:44 出处:网络
I\'m trying to make sort of a custom dialogue, which would have a previous screen(shot) as a backgroun开发者_运维技巧d, a bit darkened and blurred, as if out of focus.

I'm trying to make sort of a custom dialogue, which would have a previous screen(shot) as a backgroun开发者_运维技巧d, a bit darkened and blurred, as if out of focus.

To do this, I guess, when the dialog is initiated, a screenshot should be taken, made darker, blurred and placed as a background for the dialogue...

What do you think would be the best way to do that?

Thanks!


There's one way, merely a trick, for blurring, that you can try easily. First downscale Bitmap to, say half the size, and then upscale it back to original size using filtering. I have no idea how this turns out though.

Here's brief code example using this idea. The smaller you scale the original bitmap, the more 'blurred' should it get. But based on a few experiments it becomes very much pixelated if you use smaller than half the size using this technique. More than I was expecting.

int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width / 2, height / 2, true);

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);

Canvas canvas = new Canvas(bitmap);

Rect scaledRect = new Rect(0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight());
Rect origRect = new Rect(0, 0, width, height);
canvas.drawBitmap(scaledBitmap, scaledRect, origRect, paint);
paint.setColor(Color.argb(80, 0, 0, 0));
canvas.drawRect(origRect, paint);

Last two lines draw a semi-transparent black overlay over the Bitmap making it darker. Also BlurMaskFilter was not much a go as it works only on alpha channel. Unfortunately. Or maybe I just missed something in its usage.

0

精彩评论

暂无评论...
验证码 换一张
取 消