When you create a LiveWallpaper in Android 2.2+, you get a canvas (or, what is the same as the 3D equivalent) to draw. I would like to draw some elements using the built-in tools of the Android user interface, rather than building everything from scratch using canvas commands or loading a previously displayed bitmap of the user interface.
Converting a single image to a bitmap works fine. those. this works fine:
// For example this works: TextView view = new TextView(ctx); view.layout(0, 0, 200, 100); view.setText("test"); Bitmap b = Bitmap.createBitmap( 200, 100, Bitmap.Config.ARGB_8888); Canvas tempC = new Canvas(b); view.draw(tempC); c.drawBitmap(b, 200, 100, mPaint);
But converting LinearLayout with children is causing problems. You get only LinearLayout and not one of them. For example, if I set LinearLayout to a white background, I get a beautifully rendered white square, but none of the TextView children are in the bitmap. I also tried using DrawingCache with similar results.
The code I use is an example of a cube, with the only changes being an additional drawing command. LinearLayout works great as a toast or as a normal view (i.e., everything is well displayed), on the LiveWallpaper all I get is the rendered LinearLayout background.
inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); layout = (LinearLayout) inflater.inflate(com.example.android.livecubes.R.layout.testLinearLayout, null); layout.layout(0, 0, 400, 200); Bitmap b = Bitmap.createBitmap( 400, 200, Bitmap.Config.ARGB_8888); Canvas tempC = new Canvas(b); layout.draw(tempC); c.drawBitmap(b, 10, 200, mPaint);
Does anyone know if you need to do something special so that children display correctly in my bitmap? that is, do I need to somehow do something special so that the layout displays the rest of the children? Do I have to write a function to do something recursively for all children?
I could assemble everything myself, but since the display is quite static (i.e. I draw once and save a copy of the bitmap to draw on the background), it seems easier and still quite effective.
Edit: Although digging is a bit more in the layout state, it seems that the layout does not progress down the view tree (i.e. LinearLayout gets its layout computed when calling layout (), but the children have zero size (0x0)). According to Romain Guy in 2008, Android developer post . You must wait for the layout to go through or force the layout yourself. The problem is, how can I wait for the layout to transition from the wallpaper for LinearLayout, which is not tied to the root view group? And how can I manually split each child if the layout requires you to set left, top, right, bottom when I don't know what it should be.
I tried calling forceLayout for children, but it doesn't seem to help either. I'm not sure how the layout structure works behind the scenes (except that it does a two-pass layout). Is there a way to manually do this to skip the layout, that is, right now? Since this is not an event, I do not think that many normal background things happen exactly as we would like.