I am programmatically creating a view
which never gets drawn to the screen (the view
isn't inflated from XML or set on screen during an onCreate
). Instead, I want to create a bitmap of the view to use for an image. However, I cannot get a bitmap/a correct bitmap.
I have looked over the site and have used various questions such as here: Converting a view to Bitmap without displaying it in Android?. Initially, I used the following code:
public Bitmap loadBitmapFromView(View v)
{
Bitmap bitmap = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888); //v.getWidth() and v.getHeight() are 0 since view isn't drawn
Canvas canvas = new Canvas(bitmap);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); //v.getMeasuredHeight() and v.getMeasuredWidth() are zero --- not sure why?
v.draw(canvas);
return bitmap;
}
This code created a bitmap but it was always empty and didn't have the view
as an image. So then I tried the third answer down (by @Dwivedi Ji), posted for convenience:
private Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
v.setDrawingCacheEnabled(true);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache(); //This was always null
if (cacheBitmap == null) {
Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
v.setDrawingCacheEnabled(false);
return bitmap;
}
The v.getDrawingCache()
call always returns null which left me without a bitmap.
How could I get this bitmap with the view
image? I apologize for the lengthy question (kinda new to posting here, please let me know if you need additional info/clarification).
Thanks in advance!
EDIT
The class that I am try to make into a bitmap is as follows:
public class CircleView extends View
{
... //Constructors - Nothing special here
@Override protected void onDraw(Canvas canvas)
{
canvas.drawColor(0x00000000);
if(m_lineWidthPx == 0) //Previously set
return;
float centerX = getMeasuredHeight() / 2.f;
float centerY = getMeasuredHeight() / 2.f;
float radius = getMeasuredWidth() / 2.f - (m_lineWidthPx) / 2.f;
m_paint.setStrokeWidth(m_lineWidthPx); //m_paint is instantiated in the constructors
m_paint.setStyle(Paint.Style.STROKE);
m_paint.setAntiAlias(true);
canvas.drawCircle(centerX, centerY, radius, m_paint);
}
}
There isn't very much to the class, basically draws a circle. I omitted the constructors since nothing special occurs there. They just set the m_lineWidthPx
variable and instantiate the m_paint
variable.