This question already has answers here:
Get bitmap from layout
(3 answers)
Closed 7 years ago.
I have one problem in converting relativelayout to Bitmap in android.
In onCreate(), I have already converted the relativelayout to Bitmap.
(The relativelayout inclue the imageview and the other view)
And then I want to convert the relavielayout to Bitmap again after I change the image of the ImageView.
But the result is same to last converted image result.
The code :
v = (RelativeLayout) findViewById(R.id.relative_layout_id);
v.setDrawingCacheEnabled(true);
v.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, view.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, share_screen.getMeasuredWidth(), share_screen.getHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(share_screen.getDrawingCache());
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
image_view.setImageBitmap(b);
Use this code.
public Bitmap getBitmap(View view)
{
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
return bm;
}
you can create bitmap of a view by this way
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;
}
Related
I tried the following code to convert the LinearLayout to image:
public static Bitmap loadBitmapFromView(View v) {
v.measure(
View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight()
, Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
but I have this exception
java.lang.IllegalArgumentException: bitmap size exceeds 32 bits
How I can solve this problem?
Use Bitmap.Config.ARGB_8888 and make sure the resolved width and height are not 0.
Alternatively you can also use getDrawingCache
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache();
Try this:
private Bitmap generateBitmap(LinearLayout view)
{
//Provide it with a layout params. It should necessarily be wrapping the
//content as we not really going to have a parent for it.
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
//Pre-measure the view so that height and width don't remain null.
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//Assign a size and position to the view and all of its descendants
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//Create the bitmap
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
//Create a canvas with the specified bitmap to draw into
Canvas c = new Canvas(bitmap);
//Render this view (and all of its children) to the given Canvas
view.draw(c);
return bitmap;
}
public static Bitmap loadBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
return view.getDrawingCache();
}
Try this code to get Bitmap from LinearLayout in Android:
public static Bitmap CreateBitmap(View layout) {
layout.setDrawingCacheEnabled(true);
layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
layout.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layout.draw(canvas);
return bitmap;
}
Here is the code for convert view to bitmap.
public static Bitmap loadBitmapFromView(View v) {
v.measure(View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
Above code create real size bitmap properly. But all child position is not scaling with layout ratio.
I am working on an android app and I am trying to create bitmap image from view. My view creation codes are,
private View createInflatedViewFromLayout(Context context, String question, String answer, ViewGroup containerView){
View view;
//Creating view from layout inflater
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.image_export_view, (ViewGroup) containerView.getParent(), false);
//Getting layout and text views
LinearLayout layout = (LinearLayout)view.findViewById(R.id.imageExportLayout);
TextView pre_text_one = (TextView)view.findViewById(R.id.imageLayout_pre_text_one);
TextView pre_text_two = (TextView)view.findViewById(R.id.imageLayout_pre_text_two);
TextView pre_text_three = (TextView)view.findViewById(R.id.imageLayout_pre_text_three);
TextView post_text_one = (TextView)view.findViewById(R.id.imageLayout_post_text_one);
TextView post_text_two = (TextView)view.findViewById(R.id.imageLayout_post_text_two);
//Setting up typeface
pre_text_one.setTypeface(custFont);
pre_text_two.setTypeface(custFont);
pre_text_three.setTypeface(custFont);
post_text_one.setTypeface(custFont);
post_text_two.setTypeface(custFont);
//Setting up text and answer in text views
post_text_one.setText(question);
post_text_two.setText(answer);
//To get random number so we can get color from colors array
Random r = new Random();
int Low = 0;
int High = 5;
int Result = r.nextInt(High-Low) + Low;
layout.setBackgroundColor(Color.parseColor(colors[Result][0]));
post_text_one.setTextColor(Color.parseColor(colors[Result][1]));
post_text_two.setTextColor(Color.parseColor(colors[Result][1]));
return view;
}
So this function is working properly and creating view. I have verified it by codes below,
final LinearLayout testLo = (LinearLayout)convertView.findViewById(R.id.test_lo);
if(testLo.getChildCount() > 0)
testLo.removeAllViewsInLayout();
View lv = createInflatedViewFromLayout(context,
questions[(Integer)v.getTag()],
answers[(Integer)v.getTag()],
finalConvertView);
testLo.addView(lv, 0);
My bitmap image creation function is
public static Bitmap loadBitmapFromView(View v, Context ctx) {
Bitmap b = Bitmap.createBitmap(
2000, 2000, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, 2000, 2000);
v.draw(c);
return b;
}
and when I check the image with these codes,
Bitmap bmp = loadBitmapFromView(testLo.findViewById(0), context);
ImageView image = new ImageView(context);
image.setImageBitmap(bmp);
testLo.addView(image);
All I get is colored background but no text from view. And problem I am facing with with bitmap creation function is that I have to give height and width manually.
I mean if I use these codes,
Bitmap b = Bitmap.createBitmap(
v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getWidth(), v.getHeight());
or these codes,
Bitmap b = Bitmap.createBitmap(
v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
it is crashing app by throwing error that height and width needs to be greater than 0. It is returning -1, -2 etc.
To create bitmap image I have also used below line of code,
Bitmap bmp = loadBitmapFromView(lv, context);
Can anyone tell me where I have problem? What do I need to change to get height, width and create image from layout view.
I'm not sure but I used this boilerplate code for creating bitmaps from my screen - in other words the code below will actually take a screenshot of your phone:
final View v = getWindow().getDecorView().getRootView();
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
If you only want to take a bitmap of a specific view, like a TextView or ImageView, you can just do this:
TextView v = (TextView) findViewById(R.id.example);
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
In my code, I have a LinearLayout with many TextViews (like a table). And I try to convert this to Bitmap. there is my code:
public static Bitmap convertToBitmap(LinearLayout view){
view.measure(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
return bitmap;
}
But, there is a problem.
Views witch has setSingleLine(true) not display text in bitmap.
Exactly, the problem is in setHorizontallyScrolling(true), which is calling in setSingleLine code...
I try use .setScrollTo(X), but what exactly X must be. X = 0 - not works.
Is there any, who knows how fix it?
EDIT: TextViews has fixed Width.
EDIT: Problem is only for TextViews with Gravity = Center. With gravity Left all's ok.
Try one of this code? It's a bit different from the one you posted above.
/**
* this actually also provided by Romain Guy, but let's try another for performance improvements
*/
public static Bitmap getBitmapFromView(View view, int width, int height) {
view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// Build the Drawing Cache
view.buildDrawingCache();
// Create Bitmap
Bitmap drawingCache = view.getDrawingCache();
if (drawingCache == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
drawingCache.recycle();
view.setDrawingCacheEnabled(false);
return bitmap;
}
/**
* This method provided by Romain Guy, so it should do the job better, especially it includes case for listViews
*/
public static Bitmap getBitmapFromView(View view, int width, int height) {
//Pre-measure the view so that height and width don't remain null.
view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
//Assign a size and position to the view and all of its descendants
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// Create bitmap
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
//Create a canvas with the specified bitmap to draw into
Canvas canvas = new Canvas(bitmap);
// if it's scrollView we get gull size
canvas.translate(-view.getScrollX(), -view.getScrollY());
//Render this view (and all of its children) to the given Canvas
view.draw(canvas);
return bitmap;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting a view to Bitmap without displaying it in Android?
I am trying to convert the view into bitmap from following reference link
link text
Now the problem is how can i get the bitmap that is being converted from view only. in the example author has used relativelayout.dispatchDraw(c) but this line is giving me compile time error i.e.
The method dispatchDraw(Canvas) from
the type ViewGroup is not visible
Here is my code and i have written the following code inside onCreate function
Canvas c=null;
//Create Layout
RelativeLayout relativeView ;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeView = new RelativeLayout(this);
relativeView.setLayoutParams(lp);
//Background of Layout
Bitmap viewBgrnd = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack);
relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd));
//associated with canvas
Bitmap returnedBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
c = new Canvas(returnedBitmap);
Paint paint = new Paint();
//Create Imageview that holds image
ImageView newImage = new ImageView(this);
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink);
newImage.setImageBitmap(srcBitmap);
TextView newText = new TextView(this);
newText.setText("This is the text that its going to appear");
c.drawBitmap(viewBgrnd, 0, 0, paint);
relativeView.layout(100, 0, 256, 256);
relativeView.addView(newImage);
relativeView.addView(newText);
// here i am getting compile time error so for timing i have replaced this line
// with relativeView.draw(c);
relativeView.dispatchDraw(c);
Here the returnedBitmap should contain the image of (ImageView and TextView) but this bitmap contains only bacground bitmap of relativeView i.e. bgblack
here is my solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Enjoy :)
This works for me:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
I believe the view has to be visible (ie. getVisiblity() == View.VISIBLE). If you are trying to capture it but hide it to the user at the same time, you could move it off screen or put something over it.