Resize Image In Android Drawable
I have full resolution images in drawable folder need to resize according to phone screen size how to do that ?
Solution 1:
Do it like this
/************************ Calculations for Image Sizing *********************************/public Drawable ResizeImage(int imageID) {
//Get device dimensions Displaydisplay= getWindowManager().getDefaultDisplay();
doubledeviceWidth= display.getWidth();
BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(imageID);
doubleimageHeight= bd.getBitmap().getHeight();
doubleimageWidth= bd.getBitmap().getWidth();
doubleratio= deviceWidth / imageWidth;
intnewImageHeight= (int) (imageHeight * ratio);
BitmapbMap= BitmapFactory.decodeResource(getResources(), imageID);
Drawabledrawable=newBitmapDrawable(this.getResources(),getResizedBitmap(bMap,newImageHeight,(int) deviceWidth));
return drawable;
}
/************************ Resize Bitmap *********************************/public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
intwidth= bm.getWidth();
intheight= bm.getHeight();
floatscaleWidth= ((float) newWidth) / width;
floatscaleHeight= ((float) newHeight) / height;
// create a matrix for the manipulation Matrixmatrix=newMatrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap BitmapresizedBitmap= Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Solution 2:
to get the width you should do this :
Displaydisplay= getWindowManager().getDefaultDisplay();
double deviceWidth;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR1)
deviceWidth= display.getWidth();
else {
display.getSize(size);
deviceWidth=size.x;
}
Post a Comment for "Resize Image In Android Drawable"