Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resize and position Drawables inside a LayerDrawable in Android

I have a LayerDrawable, which should display 5 Bitmaps. 1 background and 4 Icons (which can be different for every LayerDrawable) like a Grid. Unfortunately these Icons don't have the same width than height. My Problem is to resize them, while keeping their ratio and place them inside their Position (1-4 Icons, 5 background):

==================
|      |  |      |
|   1  |  |   2  |
|------.5 .------|
|------.  .------|
|   3  |  |   4  |
|      |  |      |
==================

If I resize these Bitmaps, give them Gravity.CENTER they won't fit anymore inside the Bounds of the Background Rectangle. If I leave Gravity.CENTER they seem to get rescaled as soon as I call layerDrawable.setLayerInset(numlayer, left, top, right, bottom). What is the right way to do it? How can I keep their ratio while placing them correctly? What I also couldn't understand so far is why getIntrinsicWidth/Height from BitmapDrawable is returning something else (smaller) than getWidth/Height from original Bitmap.
What I tried:
1. Getting them from XML, putting a 'dummy-drawable' inside and replace it with result described above. 2. Create LayerDrawable inside my Program passing all drawables inside. It seems that setLayerInset has no affect, or I only see the Background (which was Element 0 inside my Drawable Array)
3. Extending LayerDrawable and try to keep track on anything myself without reasonable results

like image 683
Rafael T Avatar asked Nov 14 '22 19:11

Rafael T


1 Answers

The BitmapDrawable, dy default, fills its bounds with the given Bitmap. If you start using Gravity, the Bitmap will be position according to the given Gravity. I suppose in your case, the best choice is to create your own Drawable.

Concerning the getIntrinsic[Height|Width] methods, they return the actual size of your underlying Bitmap which may be different if the system resized it it while loading it (an hdpi image loaded on an mdpi screen for instance).

like image 153
Cyril Mottier Avatar answered Dec 04 '22 21:12

Cyril Mottier