Android - getDrawingCache - black screen - android

I've read pretty much every post on this topic, but none of them seem to help.
I'm trying to capture a screen shot of the current screen. For this I'm using getDrawingCache. Here's my code:
mRootView.get().setDrawingCacheEnabled(true);
mRootView.get().buildDrawingCache();
Bitmap screenShot = Bitmap.createBitmap(mRootView.get().getDrawingCache());
mRootView.get().setDrawingCacheEnabled(false);
The resulting bitmap is always black.
mRootView is a weak-reference to the drawer layout, which is my root view.
Here's what I've tried:
Adding a measure and layout call (although this shouldn't be needed since this code runs when a button is pressed, so the view should already be layed out).
Setting the layer type to LAYER_TYPE_NONE before calling setDrawingCacheEnabled(true)
Using a different view as the root view (for example, a ViewPager inside the DrawerLayout).
Nothing seems to work and I've run out of ideas.

You can draw your view to canvas-
pass your main layout reference to this method-
Bitmap file = save(layout);
Bitmap save(View v)
{
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}

If above solution not working so just follow this link might be helpful convert view to bitmap

Related

how to convert Layout view to Bitmap excluding some components

in my layout I have a background, button and a textView, the button here serves to convert my view to bitmap and save it
I managed to do that but my png Image contains the save button as well,
I'm using this line to get the drawing from my layout :
Bitmap cache = vw.getDrawingCache();
What I want to achieve is that before getting the screen from the view , I want it to not consider my button.
Thanks in advance.
Using canvas to draw layout like this
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layout.draw(canvas);
Have you looked at setting the component to visibility gone?
Button saveButton = (Button) findViewById(R.id.save_button);
saveButton.setVisibility(View.GONE);
Then after you get your snapshot make sure you do
saveButton.setVisibility(View.VISIBLE);
The problem with this solution is that the screenshot will still contain the gap where the button was. The view does not get redrawn before the bitmap is saved. I have tried calling invalidate() and requestLayout() with no success (which is why I found your question).
Also, another alternative is to do something like what Anand said by drawing it to a Canvas. This is useful if your view is larger than the screen. By drawing it to a Canvas you can get the entire view. By going this route, you can have a container with your background and TextView and have the button outside that container. You can then get just the view with the container. (I can't use this solution since my button is in the middle of my desired data)
View v = findViewById(R.id.my_view); // the view containing your text view
// not needed since it is outside your my_view now
// v.findViewById(R.id.save_button).setVisibility(View.GONE);
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
// not needed since it is outside your my_view now
// v.findViewById(R.id.save_button).setVisibility(View.VISIBLE);
// your Bitmap "b" now contains the image you want

Can I turn an XML-layout into a drawable?

So I want to put an xml-layout as a background to a view, but then it has to be a drawable. So is there any way to turn an it into a drawable?
I've also tried inflating it, but nothing works the way I want :(
Thx in advance.
Basically like #CommonsWare said.
1. Get the LayoutInflator of the activity
2. inflate the layout like inflater.inflate(R.layout.sample,null,false)
3. Create a canvas, that saves all the drawings into a defined bitmap like this
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Draw the created view into the canvas like this
View v = inflater.inflate(R.layout.sample,null,false); // inflate view here
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
Use the bitmap, the canvas did draw
Consider saving the bitmap locally in terms of redrawing of the activity, see following explanation:
Depending on how dynamically and often you want to create this background via canvas you should furthermore consider to save the created bitmap somewhere (temporarily), because each time the layout changes (e.g. checkbox selection), the whole canvas has to be redrawn and the inflating process needs to be done a second time too, what can lead to runtime lags in the UI.
So just save this bitmap somewhere and reload it maybe, that's what i did in am similar case.

getDrawingCache() returns null

I'm working on a simple painting app and trying to implement the functionality of giving more space to draw when a user requests, which I thought could be done by simply enabling scrolling of my CustomView class(which is contained in a LinearLayout then in a ScrollView class). If I didn't resize the CustomView class by running Chunk 1, Chunk 2 works fine(It simply saves one screen of drawing. At this time, there is no scrolling.) A bummer! Chunk 2 fails(view.getDrawingCache() returns null) when Chunk 1 is run (At this time, scrolling is enabled). I want to save the entire view including the part left out due to scrolling.
Chunk 1:
CustomView view = (CustomView) findViewById(R.id.customViewId);
height = 2 * height;
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
Chunk 2:
CustomView view = (CustomView) findViewById(R.id.customViewId);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
These two chunks of codes are independent and small parts in two different methods.
Edited: SOLVED
After billions of Google queries, the problem is now SOLVED ;)
I referred to this thread, https://groups.google.com/forum/?fromgroups=#!topic/android-developers/VQM5WmxPilM.
What I didn't understand was getDrawingCache() method has the limit on the View class's size(width and height). If the View class is too big, getDrawingCache() simply returns null.
So, the solution was not to use the method and instead do it like below.
CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
view.draw(bitmapHolder);
// bitmap now contains the data we need! Do whatever with it!
You need to call buildDrawingCache(), before being able to use the bitmap.
The setDrawingCache(true) thing just sets the flag and waits for the next drawing pass in order to create the cache bitmap.
Also don't forget to call destroyDrawingCache() when you no longer need it.

flipboard in Android

I want to use clipboard effect on android. I've seen some source codes about it. But all of them are simply flipping over some static pictures(Bitmap).I want to flip a view, and the view changes dynamically. How can I flip over this sort of view? I've tried to take a snapshot of the view, then use the snapshot to flip. But it seems that I can only snapshot a view after it presents on screen. How can I get the Bitmap of a view without present the view on screen?
One way is to draw your view hierarchy onto a bitmap and then use that for the animation. This blog post contains a few simple instructions. Basically it boils down to:
Bitmap returnedBitmap = Bitmap.createBitmap(screenWidth, screenHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(auxBitmap);
myview.draw(canvas);
Where myview is the view hierarchy you wish to draw (either created in code or an inflated layout) and returnedBitmap is your bitmap with the view on it.

Bitmap from View not displayed on Android

I am trying to create a bitmap from a View that is not displayed yet to set it as a texture with OpenGL in android.
The problem is that when I create my bitmap the layout parameters of my View have 0 value cause the onLayout() function has not been called yet.
Is there a way to "simulate" a layout or do a "background layout" to get the right layout params ?
I was thinking about a Frame layout having two views, the background one would be used to create my bitmap as the layout would have been done.
Thanks.
You indeed need to layout your View before drawing it into a Bitmap. Simply call
myView.measure(...);
then
myView.layout(0, 0, myView.getMeasuredWidth(), myView.getMeasuredHeight());
I have posted an example on how to do this in one of the following presentations: http://www.curious-creature.org/2010/12/02/android-graphics-animations-and-tips-tricks/
So, some edits after the question was clarified :)
I suggest you create the bitmap independently of the view, then scale it to the same size as the view once the view is created. Create an arbitrary size bitmap that allows you to render what you want to it ..
// This in some class that renders your bitmap independently, and can be
// queried to get the bitmap ...
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444);
// render something to your bitmap here
Then after your view is created, take the precreated bitmap and rescale it to the right size (I've not actually checked this by compiling it -- might be errors):
Rect original = new Rect(0, 0, 100, 100);
RectF destination = new RectF(0.0, 0.0, (float)myView.getWidth(), (float)myView.getHeight());
Canvas canvas = new Canvas();
canvas.drawBitmap(myRenderingClass.b, original, destination, null);
myView.draw(canvas);

Categories

Resources