Bitmap from view not showing on Android

I am trying to create a bitmap from a view that is not yet displayed in order to set it as a texture with OpenGL in android. The problem is that when creating a bitmap, the layout options of my view are set to 0, because the onLayout () function has not yet been called. Is there a way to "simulate" the layout or make a "background layout" to get the correct layout options? I was thinking of a frame layout having two views, the background would be used to create my bitmap, as the layout would be done.

Thanks.

+4
android layout bitmap
source share
2 answers

You really need to expand your view before adding it to the bitmap. Just call myView.measure(...);

then

myView.layout(0, 0, myView.getMeasuredWidth(), myView.getMeasuredHeight());

I posted an example of how to do this in one of the following presentations: http://www.curious-creature.org/2010/12/02/android-graphics-animations-and-tips-tricks/

+19
source share

So, some changes after the question was clarified :)

I suggest you create a bitmap regardless of the view, and then scale it to the same size as the view after creating the view. Create a bitmap of arbitrary size that allows you to display what you want.

  // 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 has been created, take the pre-processed bitmap and resize it to the desired size (I did not actually check this by compiling it - maybe 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);
+1
source share

All Articles