这是我的解决方案:
public static Bitmap getResizedClippedBitmap (Bitmap bm, int newWidth, int newHeight) {
Bitmap targetBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
float originalWidth = bm.getWidth();
float originalHeight = bm.getHeight();
float scale, xTranslation = 0.0f, yTranslation = 0.0f;
if (originalWidth > originalHeight) {
scale = newHeight/originalHeight;
xTranslation = (newWidth - originalWidth * scale)/2.0f;
}
else {
scale = newWidth / originalWidth;
yTranslation = (newHeight - originalHeight * scale)/2.0f;
}
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(bm, transformation, paint);
return targetBitmap;
}
您可以在此处找到其他一些实用程序:BitmapUtils.java
希望对您有所帮助。