Suppose I have a hexagon:
![](https://www.manongdao.com/static/images/pcload.jpg)
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:
![](https://www.manongdao.com/static/images/pcload.jpg)
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.
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:
![](https://www.manongdao.com/static/images/pcload.jpg)
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:
![](https://www.manongdao.com/static/images/pcload.jpg)
which avoids the problem of drawing two anti-aliased diagonal lines next to each other.
Why dont use this one instead?
Bitmap.createScaledBitmap(decodedBitmap, targetWidth, targetHeight, true);
You could try a hqx resizing algorithm:
![](https://www.manongdao.com/static/images/pcload.jpg)
Alternatively, you could paint onto a bigger surface, and scale that surface altogether.
I'm resizing images as follows:
String url = ""; //replace with path to your image
int imageDimension = 48; // replace with required image dimension
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(url), null, o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = imageDimension;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true){
if(width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Drawable drawable = new BitmapDrawable(context.getResources(), BitmapFactory.decodeStream(new FileInputStream(url), null, o2));