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:
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