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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With