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.
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.
draw()
it into aBitmap
-backedCanvas
. Step #3: Wrap theBitmap
in aBitmapDrawable
. Note that the user may be confused as to why the widgets in your background do not respond to touch events, if your background contains interactive widgets.BitmapDrawable
is not deprecated. Some constructors are. You would use the constructor that takes aResources
and aBitmap
as parameters.