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
384 views
in Technique[技术] by (71.8m points)

android - Set blurred BitmapDrawable as background of rounded CardView

I'm currently creating an app which shows the user some data on CardViews. Now I want to give the user the option to edit or delete the data by interacting with the CardViews specifically when performing a LongClick on the View. After the LongClick I want to replace the background with a blurred Screenshot of the original CardView. I have already achieved this by using this approach: https://medium.com/@arekk/how-to-preview-image-on-long-click-and-blur-background-351737f5feda

And the code looks like this

Getting the Screenshot:

public Bitmap getViewScreenshot(View view) {
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        return bitmap;
    }

Bluring the Screenshot:

private static Bitmap blurBitmap(Context context, Bitmap bitmap) {
        float bitmapScale = 0.3f;
        float blurRadius = 10f;


        int width = Math.round(bitmap.getWidth() * bitmapScale);
        int height = Math.round(bitmap.getHeight() * bitmapScale);

        Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);






        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(blurRadius);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);


        return outputBitmap;
    }

And getting a BitmapDrawable that can be set as the background of the CardView:

    public BitmapDrawable getBlurredScreenDrawable(Context context, View screen){
        Bitmap screenshot = getViewScreenshot(screen);
        Bitmap blurred = blurBitmap(context, screenshot);
        return new BitmapDrawable(getContext().getResources(), blurred);
    }

The problem now is that the returned BitmapDrawable is the full size of the original rectangular CardView yet the CardView has rounded corners. Therefor the final result looks like this:

enter image description here

enter image description here

Is there a way to round the corners of the Drawable as well?

Thanks in advance for answers and have a happy new year!

Edit:

I have already tried using the solution presented here: Bitmap image with rounded corners with stroke integrating it into the getBlurredScreenDrawable method like this

public BitmapDrawable getBlurredScreenDrawable(Context context, View screen, int radius){
        Bitmap screenshot = getViewScreenshot(screen);
        Bitmap blurred = blurBitmap(context, screenshot, radius);
        Canvas canvas = new Canvas(blurred);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, screenshot.getWidth(), screenshot.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = radius;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(screenshot, rect, rect, paint);
        
        return new BitmapDrawable(getContext().getResources(), blurred);
    }

But with this the CardView ends up looking like this

enter image description here


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

1 Reply

0 votes
by (71.8m points)
等待大神答复

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

...