2

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.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • What background do you want to set? – Hello World Jun 29 '16 at 11:35
  • Step #1: Inflate it. Step #2: `draw()` it into a `Bitmap`-backed `Canvas`. Step #3: Wrap the `Bitmap` in a `BitmapDrawable`. 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. – CommonsWare Jun 29 '16 at 11:51
  • @CommonsWare I'll try this. But I think BitmapDrawable is deprecated? Should I use it anyway? – David Dahlgren Jun 29 '16 at 11:55
  • `BitmapDrawable` is not deprecated. Some constructors are. You would use the constructor that takes a `Resources` and a `Bitmap` as parameters. – CommonsWare Jun 29 '16 at 11:57

1 Answers1

5

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);
  1. 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);
    
  2. Use the bitmap, the canvas did draw

  3. 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.

gianlucaparadise
  • 1,678
  • 20
  • 32
Thomas Richter
  • 833
  • 8
  • 20
  • Okay, but I don't get how to create the bitmap? I mean 5/8 constructors are deprecated, and the remaining ones are BitmapDrawable(Resources res, Bitmap bitmap), BitmapDrawable(Resources res, String filepath) and BitmapDrawable(Resources res, InputStream is) I don't have a filepath or an old bitmap, so only the one with inputStream remains, which I don't have a clue how to use. Sorry I'm new to this. – David Dahlgren Jun 29 '16 at 13:18
  • Updated my answer. This is the way i did it. If you call v.layout() make sure use adequat measerument to define the infalted views size! – Thomas Richter Jun 29 '16 at 13:43
  • 1
    Thank you for helping me, but I can't seem to get it to work. What I do is that I 1. Inflate my xml-layout. 2. Create a bitmap the way you described 3. Wrap it so it becomes a drawable ( BitmapDrawable bmpDrawable = new BitmapDrawable(getResources(), bmp); ) 4. Create canvas and draw my inflated view on it, also specify size of my inflated view. 5. Finally I set bmpDrawable as background in the view I want. The view I want to put the background in is a view in a GridLayout. On my screen that specific view does change it's color, but not the way as my xml :/ Not sure what to do. – David Dahlgren Jun 29 '16 at 14:19
  • Could you provide a screenshot? – Thomas Richter Jun 30 '16 at 08:19
  • When i get you right, you define in XML a backgroudcolor for the view, that shall be converted to bitmap. But by converting it, the backgroundcolor changes? I guess the problem is the default backgroundcolor of the bitmap.
    For example try: canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    But instead of Color.Transparent use your own color. Alternatively draw a rectangle with the desired backgroundcolor to the canvs and make sure that this rectangle has the same size like the canvas. When you have colored the background, draw the view on top ;) One of the ways should work:P
    – Thomas Richter Jun 30 '16 at 08:29