2

I usually download images that are larger in size and shrink them using an imageview but lately Im trying to deal with my apps not working on a lesser network connection so I was wondering how can I increase the size of an image once it gets to the device. I tried resizing the image once it was in an imageview but the imageview will get no larger than the original image. Im sure theres a really easy way to increase or blow up an image on the device but I havent come across it yet.

So.....how can I increase the size of an image. Id like to blow it up and use it in an imageview but the images Im dealing with are only 128X256 and Id like to expand them to about 512X1024.

James andresakis
  • 5,335
  • 9
  • 53
  • 88

4 Answers4

5

Try using this method:

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {   
if(bitmapToScale == null)
    return null;
//get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();

// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);

// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);  } 

refer to my answer in: ImageView OutofMemoryException

Community
  • 1
  • 1
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • Thanks for the method, I was thinking it would be something simple like that but after programming all day my brain ran out of steam lol. I just have one question about your method.....why in your call to matrix.postScale() are you dividing the new dimensions by the old ones? Wouldnt that make the matrix return a value that was much smaller than my goal of expanding the image? – James andresakis Jun 26 '12 at 20:39
2

Use matrix to resize the bitmap.

Check this Resize Bitmap

Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
0

If you want to scale the Bitmap manually, there is a Bitmap.createScaledBitmap() method that you can use for this.
If you want the ImageView to handle this, you have to set the layout_width/layout_height to something other than wrap_content or it will always shrink to the size of the Bitmap. Then you need to change the scaleType attribute to a type that actually scales the bitmap.

Jave
  • 31,598
  • 14
  • 77
  • 90
  • 1
    I have tried to use this but trying to use that method to expand or increase the size of an image threw an error for me. It was something about not being able to scale larger than the original image. – James andresakis Jun 26 '12 at 20:40
0

The functionality to scale image up or down is readily available via android.graphics library:

Bitmap bitmapScaled = Bitmap.createScaledBitmap(Bitmap bitmapOrig, int widthNew, int heightNew, boolean filter);
Panini Luncher
  • 639
  • 8
  • 10