1

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:

  1. Load it as BitmapDrawable, enabled AntiAlias and FilterBitmap and called it's draw method.
  2. Load it using BitmapFactory.decodeResource with opts.inScaled set to fales and drawn to canvas using Matrix.
  3. 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:

Scaled down on Android with filterBitmap=true

It seems that Paint.setFilterBitmap does have some effect, because without it it's even a bit worse:

Scaled down on Android with filterBitmap=false

This is the scaled down version using just any image editor using the same original image, which is a lot smoother.

Scaled down with image editor

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.

Oliv
  • 10,221
  • 3
  • 55
  • 76
  • Have you tried to use `Bitmap.Config.ARGB_8888` instead of `Bitmap.Config.RGB_565` ? – hardartcore Jan 25 '16 at 09:55
  • 1
    `This is the scaled down version using just any image editor using the same original image,` which is why you should provide your scaled images into several drawable folders. Or use SVG files (or even VectorDrawables). – Phantômaxx Jan 25 '16 at 10:14
  • I have tried, no luck. More precisely, I tried to load the `logoImage` with `ARGB_8888` config and to scale it using `Bitmap.createScaledBitmap` and saved that to file and the files are identical with the previous one. – Oliv Jan 25 '16 at 10:16
  • @HrundiV.Bakshi I know that, but it adds more resources to manage, larger APK, and what if the target size is not static? Answer the question. – Oliv Jan 25 '16 at 10:18
  • `SVG` files, being vectorial, are (by definition) scalable. Therefore, you'd need only one resource and scale it as per your current screen size. `VectorDrawables`, being a *subset* of SVG files, are as well. – Phantômaxx Jan 25 '16 at 10:26
  • But: `VectorDrawables` are available only for android>5, which is prohibitive. SVGs are, AFAIK, supported only as a third party build plugin, which generates PNG files in all resolutions during build, so no scaling at runtime. – Oliv Jan 25 '16 at 11:04
  • **Wrong**. And **wrong**. `VectorDrawables` are available to the older API levels through the **support library**. `SVG` files (yes, by using a FREE library) will be scaled **at runtime** and generate a BitmapDrawable to be inserted into your Views. – Phantômaxx Jan 25 '16 at 13:16
  • @HrundiV.Bakshi could hou point me to the support libray? I've only found unofficial supported for api14+ and some work-in-progress for official support library. Also could not find support for SVG. – Oliv Jan 25 '16 at 13:26
  • SVG files are not supported through any API library, yet (hopefully they'll replace VectorDrawables with SVGDrawables?). Only by 3rd party free libraries, like android-svg and svg-andoid. – Phantômaxx Jan 25 '16 at 13:28
  • And VectorDrawables? – Oliv Jan 25 '16 at 13:30
  • I was wrong about those ones. The ones will work as you said: Android Studio will convert them to PNGs for older APIs. See: http://stackoverflow.com/a/32908702/2649012 – Phantômaxx Jan 25 '16 at 13:31
  • Ok, so we are back, nothing new here. If you know, answer the question :). – Oliv Jan 25 '16 at 13:35

0 Answers0