试试这个..
private Bitmap performResize(Bitmap bitmap, int requiredWidth,
int requiredHeight) {
int imageWidth = bitmap.getWidth();
int imageHeight = bitmap.getHeight();
float differenceWidth = requiredWidth - imageWidth;
float percentage = differenceWidth / imageWidth * 100;
float estimatedheight = imageHeight + (percentage * imageHeight / 100);
float estimatedwidth = requiredWidth;
if (estimatedheight < requiredHeight) {
float incresePercentage = (float) (requiredHeight - estimatedheight);
percentage += (incresePercentage / imageHeight * 100);
estimatedheight = imageHeight + (percentage * imageHeight / 100);
estimatedwidth = imageWidth + (percentage * imageWidth / 100);
}
bitmap = ScalingUtilities.performResize(bitmap, (int) estimatedheight,
(int) estimatedwidth);
if (bitmap.getHeight() < requiredHeight)
{
} else {
int xCropPosition = (int) ((bitmap.getWidth() - requiredWidth) / 2);
int yCropPosition = (int) ((bitmap.getHeight() - requiredHeight) / 2);
bitmap = Bitmap.createBitmap(bitmap, xCropPosition, yCropPosition,
(int) requiredWidth, (int) requiredHeight);
}
return bitmap;
}
utility file
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
public class ScalingUtilities {
public static Bitmap decodeResource(Resources res, String path, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
dstHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options);
return unscaledBitmap;
}
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(),
dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(),
Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static enum ScalingLogic {
CROP, FIT
}
public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)dstWidth / (float)dstHeight;
if (srcAspect > dstAspect) {
return srcWidth / dstWidth;
} else {
return srcHeight / dstHeight;
}
} else {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)dstWidth / (float)dstHeight;
if (srcAspect > dstAspect) {
return srcHeight / dstHeight;
} else {
return srcWidth / dstWidth;
}
}
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float)srcWidth / (float)srcHeight;
final float dstAspect = (float)dstWidth / (float)dstHeight;
if (srcAspect > dstAspect) {
final int srcRectWidth = (int)(srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
} else {
final int srcRectHeight = (int)(srcWidth / dstAspect);
final int scrRectTop = (int)(srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {
{
return new Rect(0, 0, dstWidth, dstHeight);
}
}
public static Bitmap performResize(Bitmap unscaledBitmap,int height,int width)
{
Bitmap scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, width, height, ScalingUtilities.ScalingLogic.FIT);
unscaledBitmap.recycle();
return scaledBitmap;
}
public static Bitmap updateImageFromGallary(Context ctx,String imgPath,int reqWidth, int reqHeight)
{
Bitmap cameraBitmap=null;
try
{
cameraBitmap = decodeSampledBitmapFromResource(ctx.getResources(),imgPath,reqWidth,reqHeight);
}
catch(OutOfMemoryError outMemeoryError)
{
}
return cameraBitmap;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, String path,int reqWidth, int reqHeight)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPurgeable = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
public static float convertPixelsToDp(float px, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
public static int convertToPx(int input,Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
Log.e("textSize","textSize scale value "+scale);
return (int) (input * scale + 0.5f);
}
}