set bitmap which was created from RelativeLayout to imageView - android

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

Related

Get Android view as Bitmap to save it on SD

I'm trying to save the path that is being drawn on a View but I just can't figure out how to do this.
Here is what I'm doing in my Activity to create the View and set it as content:
View accPathView = new AccPathView(this, steps);
setContentView(accPathView);
Then in the onDraw method of my View class, I'm simply creating a path and drawing it on the canvas I received as parameter:
But then, when I try to get the bitmap of my view with getDrawingCache(), it's always null and creates an empty image on my SD. I tried
accPathView.setDrawingCacheEnabled(true);
accPathView.buildDrawingCache(true);
Unfortunately it didn't change anything and I still get an empty bitmap.
You may try this:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
EDIT:
How do you the above method?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View accPathView = new AccPathView(this, steps);
setContentView(accPathView);
accPathView.post(new Runnable() {
public void run() {
Bitmap viewBitmap = getBitmapFromView(accPathView);
}
});
// your remaining oncreate
}

Getting screenshot from a LinearLayout

I have a LinearLayout that contains a lot of sub LinearLayouts which contain TextViews. I want to get a screenshot from the parent LinearLayout to get a full view of my "Receipt". So I tried to do that:
View v = findViewById(R.id.llReceipt);
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
the problem is the Bitmap b get a null value.
Is there any solution to solve it?
I don't know why your bitmap is null but I've used this bit before to get a screen shot of a custom view and it worked:
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;
}
Taken from here!
I found my problem:
I called the getScreenShot() Function from the onCreate() Function which doesn't work.
it should call it after this step.

Create bitmap from view makes view disappear, how to get view canvas?

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.

Posting ScrollView Content on FaceBook wall

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.

Android--Why I can't see the VIEW directly?

I've built a new view named myview including OnDraw section.
And I tried to use setContentView(myview) in Oncreate, however, it failed and showed some error.
Then I use addview function to add this view to my original layout, and setContent(R.layout.main)
Finally I can see the result on the screen.
What was the problem ? Why I can't directly use setContentView(myview) to show the result ?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myview2=new MyView2(this);
setContentView(R.layout.main2);
RelativeLayout layout= ( RelativeLayout )findViewById(R.id.dashpage);
layout.addView(myview2);
}
class MyView2 extends View{
public MyView2(Context context) {
super(context);
}
Bitmap themometer = BitmapFactory.decodeResource(getResources(),
R.drawable.the);
Matrix matrix2 = new Matrix();
Bitmap themometerbmp = Bitmap.createBitmap(themometer, 0, 0, themometer.getWidth(),
themometer.getHeight(), matrix2, true);
public void onDraw(Canvas canvas2)
{
canvas2.drawBitmap(themometerbmp,90,5,null);
}
}
I don't think setContentView is the problem. You are trying to use a drawable. Try this:
Add a function in MyView2:
public Bitmap getBitmapFromDrawable (Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Then, instead of
Bitmap themometer = BitmapFactory.decodeResource(getResources(), R.drawable.the);
use
Bitmap themometer = getBitmapFromDrawable(getDrawable(R.drawable.the));

Categories

Resources