I used ScrollView in my application. It contains a LinearLayout as child and some other views likes
TextViews,Buttons etc... as sub-views. Now I want to post the content of scrollview as an image on FaceBook wall.I could not find out a suitable post.
**My question is how to post ScrollView/Layout content on facebook wall?...**
Thanks in Advance
use this function to convert your view in to bitmap image
public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(480, 640,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;
}
then post bitmap image on facebook wall
hope help you..
I changed above code to
public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight() , 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;
}
It works fine and got bitmap of all content.
Related
I'm working on Editor Application, I need to save FrameLayout in Internal Storage,
Here I have done convert Layout to bitmap code, It's Working Fine but it gives low quality image.
Here is my code and output...
private Bitmap viewToImage(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;
}
Any suggestions to produce HD image will be appreciated
This i get low quality image
This is original image that i want in output
Thanks in advance!
I am trying to make an app like HBD wish sharing app but I want to add text to the image and share it as an image file. add text in the image in Relative Layout is simple but I want to share that image and text as an image file. my app maybe it looks like a certificate generator(sololearn app for example)
Thank You
Just pass the id of the relative layout in here and it will return a bitmap of that entire view. You can then save that as a PNG file and you're good to go.
private Bitmap getBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
Bitmap returnedBitmap = view.getDrawingCache();
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;
}
I have RelativeLayout and I want to convert it to the Bitmap like this:
public static Bitmap getBitmapFromView(View view) {
if (view != null)
{ 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;
}
else
return null;
}
then I create RelativeLayout cView in the onCreate() method dynamically and set it to the ImageView like this:
dialer = (ImageView) findViewById(R.id.imageView_ring);
dialer.setImageBitmap(getBitmapFromView(cView));
and get error:
05-20 13:48:27.509: E/AndroidRuntime(28367):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{ru.biovamp.widget/ru.biovamp.widget.TestCircleAct}:
java.lang.IllegalArgumentException: width and height must be > 0
I also tried to add bitmap to the ImageView from the onStart() method, but got the same result. What solution can I use?
I've solved my issue like this:
public Bitmap getBitmapFromView() {
this.measure(MeasureSpec.makeMeasureSpec(createNewRelativeLayoutParams().width, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(createNewRelativeLayoutParams().height, MeasureSpec.UNSPECIFIED));
this.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
Bitmap returnedBitmap = Bitmap.createBitmap(this.getMeasuredWidth(), this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
this.draw(c);
return returnedBitmap;
}
so solution is that we should to call measure() and layout() methods before drawing it.
oficial explaniton
I found two ways of creating a Bitmap from a view. But once I do that, the view disappears and I can't use it anymore. How can I redraw the view after generating my bitmap?
1st:
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;
}
2nd:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
EDIT
So, I think I understand what happens on the first one, we are basically removing the view from it's original canvas and drawing it somewhere else associated with that bitmap. Can somehow we store the original canvas and then set the view to be redrawn there?
Sorry, I'm not immensely knowledgeable on this. But I use the following code:
public Bitmap getBitmapFromView(View view, int width, int height) {
Bitmap returnedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (view==mainPage.boardView) {
canvas.drawColor(BoardView.BOARD_BG_COLOR);
} else if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
view.layout(0, 0, width, height);
view.draw(canvas);
return returnedBitmap;
}
which is so similar to yours I suspect we copy&edited from the same place.
I have no trouble with the view disappearing from the original drawing tree. Mine is called for ViewGroups rather than plain Views.
Try this.
Getting the bitmap:
// Prepping.
boolean oldWillNotCacheDrawing = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
view.setDrawingCacheEnabled(true);
// Getting the bitmap
Bitmap bmp = view.getDrawingCache();
And make sure to reset the view back to its old self.
view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);
view.setWillNotCacheDrawing(oldWillNotCacheDrawing);
return bmp;
Guy's answer works when the View has not yet been laid out in a parent view.
If the view already has been measured and laid out in a parent-view, Guy's answer may screw up your Activity's layout.
If the view hasn't yet been measured and laid out, Guy's answer works fine.
My answer would work after the View has been laid out and it won't screw up the Activity's layout since it doesn't measure and layout the View again.
In my application is a WebView widget, which opens a big page.
How do I capture the visible part of the page in WebView? capturePicture() is not suitable for it...
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}
Save your webView under bitmap. Then you save bitmap on SD card.
Use this to get your bitmap from webView :
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.TRANSPARENT);
view.draw(canvas);
return returnedBitmap;
}
Now just save it on SD and it's done