我有时会使用以下方法..我不知道这些是否对您有帮助..请检查这是否适合您
float scale = getResources().getDisplayMetrics().density;
Display display = ((WindowManager) main.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int WIDTH = display.getWidth();
int HEIGHT = display.getHeight();
public static Drawable resizeDrawable(Drawable d, float scale) {
Drawable drawable = null;
if (d != null) {
try {
Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap();
int width = 0;
int height = 0;
if (Math.min(WIDTH, HEIGHT) > 600) {
width = (int) (100 * scale + 0.5f);
height = (int) (100 * scale + 0.5f);
} else if (Math.min(WIDTH, HEIGHT) > 240) {
width = (int) (70 * scale + 0.5f);
height = (int) (70 * scale + 0.5f);
} else {
width = (int) (44 * scale + 0.5f);
height = (int) (44 * scale + 0.5f);
}
drawable = new BitmapDrawable(resizeBitmap(bitmap1,
width, height));
} catch (Exception e) {
e.printStackTrace();
}
}
return drawable;
}
请注意,resizeDrawable 方法中 if-else 条件中使用的值只是试验 n 错误(适合我的应用程序)采用的任意值。您可以根据您的目标屏幕尝试其他值
public static Bitmap resizeBitmap(final Bitmap bitmap, final int width,
final int height) {
final int oldWidth = bitmap.getWidth();
final int oldHeight = bitmap.getHeight();
final int newWidth = width;
final int newHeight = height;
// calculate the scale
final float scaleWidth = ((float) newWidth) / oldWidth;
final float scaleHeight = ((float) newHeight) / oldHeight;
// create a matrix for the manipulation
final Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// recreate the new Bitmap
final Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
oldWidth, oldHeight, matrix, true);
return resizedBitmap;
}