1

I have some different size bitmap here: and I want to resize them to same width and height (64px) each image here is what I did:

int size = Math.round(64 * getResources().getDisplayMetrics().density);
int imagesLength = 4;
Bitmap bmp = Bitmap.createBitmap(size*imagesLength, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
for (int i = 0; i < imagesLength; i++) {
    Bitmap mImage = getImage(i);
    // ...code to get bitmap...
    canvas.drawBitmap(mImage,i * size, 0, null);
}

do anyone got an idea? thanks

Navdeep Soni
  • 449
  • 4
  • 15
user3896501
  • 2,987
  • 1
  • 22
  • 25

1 Answers1

2

You can use Bitmap.createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter) to change the image to the size you want.

But this will not keep the aspect ratio intact. If thats important you might want to think of something else. Like scaling it down as you already did and adding it centered on some kind of background.

This gets you square bitmaps, no matter what:

int size = Math.round(64 * getResources().getDisplayMetrics().density);
int imagesLength = 4;
Bitmap bmp = Bitmap.createBitmap(size*imagesLength, size, Bitmap.Config.ARGB_8888); // the bitmap you paint to
Canvas canvas = new Canvas(bmp);
for (int i = 0; i < imagesLength; i++) {
    Bitmap mImage = getImage(i);

    // ...code to get bitmap...
    mImage = Bitmap.createScaledBitmap(mImage , size, size, true);

    canvas.drawBitmap(mImage,i * size, 0, null);
}

And this is for keeping the ratio but leaving gaps:

int size = Math.round(64 * getResources().getDisplayMetrics().density);
int imagesLength = 4;
Bitmap bmp = Bitmap.createBitmap(size*imagesLength, size, Bitmap.Config.ARGB_8888); // the bitmap you paint to
Canvas canvas = new Canvas(bmp);
for (int i = 0; i < imagesLength; i++) {
    Bitmap mImage = getImage(i);

    // ...code to get bitmap...
    int width = mImage.getWidth();
    int height= mImage.getHeight();
    float ratio = width/(float)height;
    if(ratio>1)
    {
        mImage = Bitmap.createScaledBitmap(mImage , size, size/ratio, true);
    }
    else
    {
        mImage = Bitmap.createScaledBitmap(mImage , size*ratio, size, true);
    }

    canvas.drawBitmap(mImage,i * size, 0, null);
}
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • What do you mean it doesn't keep the aspect ration intact, I've used this before and it worked like a charm. – vedi0boy Sep 08 '14 at 10:33
  • You can enter arbitrary values for width and height . If you don't use the same scale for both values, like the OP would have to do for the examples 1,2 and 4, you get a distorted image. – Dawnkeeper Sep 08 '14 at 11:04
  • How to keep the ColorFilter? my code is ` Bitmap.createScaledBitmap(((BitmapDrawable) image).getBitmap(), size, size, true)` but cannot apply the filter – user3896501 Sep 08 '14 at 13:45