Converting a View to Bitmap Without Displaying It in Android

Converting a view to Bitmap without displaying it in Android?

there is a way to do this. you have to create a Bitmap and a Canvas and call view.draw(canvas);

here is the code:

public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}

if the view wasn't displayed before the size of it will be zero. Its possible to measure it like this:

if (v.getMeasuredHeight() <= 0) {
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}

EDIT: according to this post, Passing WRAP_CONTENT as value to makeMeasureSpec() doesn't to do any good (although for some view classes it does work), and the recommended method is:

// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();

android converting XML View to Bitmap without showing it

Your cluster.getLayoutParams() is probably null. First, you need to measure the width/height of your inflated view and then assign to it. Do it as below:

private Bitmap createClusterBitmap(int clusterSize) {
View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster,
null);

TextView clusterSizeText = (TextView) cluster.findViewById(R.id.map_cluster_text);
clusterSizeText.setText(String.valueOf(clusterSize));

cluster.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
cluster.layout(0, 0, cluster.getMeasuredWidth(),cluster.getMeasuredHeight());

final Bitmap clusterBitmap = Bitmap.createBitmap(cluster.getMeasuredWidth(),
cluster.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(clusterBitmap);
cluster.draw(canvas);

return clusterBitmap;
}

Android Convert view to BitMap

Hi you can use this method to convert View to Bitmap

private Bitmap createBitmapFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
view.draw(canvas);

return bitmap;
}

Creating a bitmap of a View that never gets drawn

Thanks to both @Rafael Toledo and @Roman_Donchenko, I figured out the issue. The problem was that my custom view was doing the drawing in the onDraw method which doesn't get called. In my case, I simply needed to perform the drawing done in the onDraw directly to the canvas and then used the bitmap passed to the canvas. My code ended up being:

public Bitmap getBitmapFromCircleView(CircleView v)
{
Bitmap bitmap = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.drawToCanvas(canvas); //Method in the custom CircleView class, posted below
return bitmap;
}

public class CircleView extends View
{
... //Constructors

public Canvas drawToCanvas(Canvas canvas)
{
canvas.drawColor(0x00000000);

if(m_lineWidthPx == 0) //Previously set
return null;

int width = getLayoutParams().width;
int height = getLayoutParams().height;

float centerX = height / 2.f;
float centerY = height / 2.f;
float radius = width / 2.f - (m_lineWidthPx) / 2.f;
m_paint.setStrokeWidth(m_lineWidthPx);
m_paint.setStyle(Paint.Style.STROKE);
m_paint.setAntiAlias(true);
canvas.drawCircle(centerX, centerY, radius, m_paint);
return canvas;
}

@Override protected void onDraw(Canvas c)
{
... //Code here remained unchanged
}
}

How to convert View to Bitmap in android?

Activity Class In this Class I Convert the Layout(View) into Image.

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ImageActivity extends Activity {

LinearLayout layout=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convert);
layout=(LinearLayout)findViewById(R.id.layout);
((ImageView)findViewById(R.id.ImageView01)).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Bitmap map=ConvertToBitmap(layout);
Log.v("BitmapObject", map.toString());
}
});
}

protected Bitmap ConvertToBitmap(LinearLayout layout) {
Bitmap map;
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
return map=layout.getDrawingCache();
}
}


Related Topics



Leave a reply



Submit