Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize Bitmaps the optimal way in Android?

Suppose I have a hexagon:

hexagon

If I resize this to use it in my application which contains a grid of hexagons:

// ...
bgPaint = new Paint();
bgPaint.setAntiAlias(true);
bgPaint.setDither(true);

// ...
Bitmap coloredBackground = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

// ...
canvas.drawBitmap(coloredBackground, null, getAsRect(), bgPaint);

I get this:

Hexagons

getAsRect() returns a Rect object I use for drawing. What I want to achieve is to get rid of those transparent pixels at the edges. I think I'm doing something wrong but I could not find where so far. Do you have any idea how can I solve this problem?

I tried experimenting with dither and antialias but nothing changed.

like image 508
Adam Arold Avatar asked Jan 15 '23 20:01

Adam Arold


1 Answers

3 suggestions:

1

Try this: Turn off the system's scaling when you decode the resource by setting BitmapFactory.Options.inScaled to false:

Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg, options);

The inScaled flag should be turned off if you need a non-scaled version of the bitmap.

Then scale your bitmap with Bitmap.createScaledBitmap(...).

2

Another possible reason is that your tile's diagonal black lines contain different shades of grey:

This is a close up of your tile:

enter image description here

It's anti-aliased before it's resized. Any pixels not totally black may show up as lighter color in the resized lines. You could change your lines lines to be completely black (0xFF000000) and do anti-alias only after the resizing.

3

Another solution to this problem is to design your tile like so:

enter image description here

which avoids the problem of drawing two anti-aliased diagonal lines next to each other.

like image 170
onosendai Avatar answered Jan 24 '23 16:01

onosendai