Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a background tile?

Tags:

android

I'm trying to create a tiled background for a match_parent-wide container, where the tile is repeated from the parent's center, not from its left edge. I want this in order for the background to always appear centered regardless of screen width.

I noticed that Android's BitmapDrawable has a gravity attribute, but it seems to have no effect when tileMode is set to repeat or mirror. I would have expected that the bitmap is first centered in its container, then repeated outwards from the center point. Instead, it's still left-aligned and then repeated, resulting in the first tile always be fully visible but the last tile being cut off unless the screen width is a multiple of the tile's width.

UPDATE: Just noticed, "Gravity is ignored when the tile mode is enabled." (tileMode attr docs.) Any other ways to achieve this?

like image 818
Matthias Avatar asked Nov 04 '22 12:11

Matthias


1 Answers

I think this post might be interesting to you. Basically gravity doesn't work with tilemode (as we've already established) but you could create a matrix of your images and use that to create the background of your desire. So basically build the tilemode in code yourself.

like image 163
Warpzit Avatar answered Nov 12 '22 14:11

Warpzit