In my ImageView's onDraw
I am struggling to convert a Drawable object to a Bitmap (respecting scaling). I'm loading an SVG file as a PictureDrawable. Then I'm trying to apply rounded corners to the image with a BitmapShader. In order to do that I have to convert the Drawable to Bitmap. It basically works, but I'm not getting my head around the scaling procedure.
Bitmap bitmap = Bitmap.createBitmap(
picture.getIntrinsicWidth(),
picture.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888
)
Canvas canvas = new Canvas( bitmap )
// Scaling the Canvas appears to work ...
canvas.concat( getImageMatrix() )
canvas.drawPicture(
picture.getPicture,
// ... however this will not fill the viewport, as the getWidth and getHeight
// values do not reflect the scaling
new RectF( 0, 0, canvas.getWidth(), canvas.getHeight() )
)
paint.setShader( new BitmapShader( bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP ) )
canvas.drawRoundRect(
new RectF( 0, 0, bitmap.getWidth(), bitmap.getHeight() ),
radius,
radius,
paint
)
Erroneous rendering example for centerCrop scaling:
Besides the problem described in the code comments above I am wondering whether it might be possible to mask the the Picture/SVG file with drawing operations such as clipPath instead of this heavy Bitmap conversion. But it'd have to be anti-aliased, of course.
The code was originally written in Scala and loosely translated to Java for SO, so please ignore any Syntax errors