I have a class that derives from ImageView. In its onDraw method override I need to copy the view’s contents into a bitmap. The copy must contain the whole contents not just the visible portion.
To accomplish this I create a bitmap and invoke super.onDraw passing this bitmap’s canvas as the parameter. I then draw the resulting bitmap to the canvas that onDraw was passed. The code is below.
My question is how big my bitmap should be? Currently I use getWidth() and getHeight() as the bitmap’s dimensions. But this does not work because it returns the view size, not the contents size. I am looking for a way to get the size of the contents after the drawable has been resized according to the view’s scaleType.
The size of the drawable does not work because the drawable can be different from the contents dimensions.
@Override
public void onDraw(Canvas canvas)
{
if (_bitmap == null)
{
final int w = getWidth();
final int h = getHeight();
_bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
_canvas = new Canvas(_bitmap);
super.onDraw(_canvas);
}
canvas.drawBitmap(_bitmap, 0, 0, null);
}
Have you looked into
getDrawingCache(boolean autoScale)
Returns the bitmap in which this view drawing is cached.
Set autoScale to true.