I have a PNG image in app's resources, that has 950*291 pixels, placed in drawable-mdpi
directory. I want to create an in-memory Bitmap
, where I want to draw it scaled down (actually 100 pixels high). I tried many ways, but it looks pixelated.
I tried the following:
- Load it as
BitmapDrawable
, enabledAntiAlias
andFilterBitmap
and called it'sdraw
method. - Load it using
BitmapFactory.decodeResource
withopts.inScaled
set to fales and drawn to canvas usingMatrix
. - Same as previous, but first scaled it down using
Bitmap.createScaledBitmap
and then drawing to canvas without a matrix.
Neither worked. This is the image I get, it is not smooth, is pixelated:
It seems that Paint.setFilterBitmap
does have some effect, because without it it's even a bit worse:
This is the scaled down version using just any image editor using the same original image, which is a lot smoother.
Am I not able to scale images in higher quality with Android? The image clearly stands out to other antialiased text in the image.
This is my current code:
// create in-memory bitmap
Bitmap bitmap = Bitmap.createBitmap(1000, 1100, Bitmap.Config.RGB_565);
bitmap.setDensity(Bitmap.DENSITY_NONE);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setFilterBitmap(true);
paint.setAntiAlias(true);
// load the original image
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap logoBitmap = BitmapFactory.decodeResource(resources, R.drawable.main_logo, opts);
logoBitmap.setDensity(Bitmap.DENSITY_NONE);
// scale it down and draw it
Bitmap scaledLogo = Bitmap.createScaledBitmap(logoBitmap, logoWidth, logoHeight, false);
canvas.drawBitmap(scaledLogo, x, y, paint);
Tested on Android 5.0. I worked it around by adding scaled version to resources, but want to know what was wrong.