2

I have full resolution images in drawable folder need to resize according to phone screen size how to do that ?

KRUNAL DOSHI
  • 81
  • 1
  • 2
  • 10
  • This should be for what you looking; http://developer.android.com/guide/practices/screens_support.html – zennon Dec 03 '12 at 14:11

2 Answers2

4

Do it like this

/************************ Calculations for Image Sizing *********************************/ 
public Drawable ResizeImage (int imageID) { 
//Get device dimensions 
Display display = getWindowManager().getDefaultDisplay(); 
double deviceWidth = display.getWidth(); 

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(imageID); 
double imageHeight = bd.getBitmap().getHeight(); 
double imageWidth = bd.getBitmap().getWidth(); 

double ratio = deviceWidth / imageWidth; 
int newImageHeight = (int) (imageHeight * ratio); 

Bitmap bMap = BitmapFactory.decodeResource(getResources(), imageID); 
Drawable drawable = new BitmapDrawable(this.getResources(),getResizedBitmap(bMap,newImageHeight,(int) deviceWidth)); 

return drawable; 
} 

/************************ Resize Bitmap *********************************/ 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

int width = bm.getWidth(); 
int height = bm.getHeight(); 

float scaleWidth = ((float) newWidth) / width; 
float scaleHeight = ((float) newHeight) / height; 

// create a matrix for the manipulation 
Matrix matrix = new Matrix(); 

// resize the bit map 
matrix.postScale(scaleWidth, scaleHeight); 

// recreate the new Bitmap 
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

return resizedBitmap; 
}
Androider
  • 2,884
  • 5
  • 28
  • 47
-1

to get the width you should do this :

Display display = getWindowManager().getDefaultDisplay();
double deviceWidth;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR1)
            deviceWidth= display.getWidth();
        else {
            display.getSize(size);
            deviceWidth=size.x;
        }
JCDecary
  • 557
  • 1
  • 7
  • 18