奇怪的事情发生了——至少我不明白。
我有一张图片(宽:120 像素,高:63 像素),显示在一个 ImageButton 上。我拥有的该图像的唯一变体放在 drawable-hdpi 中(并且没有其他可绘制目录)。这张图片和每个分辨率(密度)都很好,Android 很好地处理了它。
但是,问题是当我从相册(例如一些非常大的照片)中拍摄照片时,将其缩放到按钮的尺寸(前面提到过,w: 120px h: 63px)并将那个小图像设置为 ImageButton 背景。
如果我在中等密度(160)的模拟器上测试它是可以的,但是当在具有高密度的设备上测试时,按钮会被调整大小,因为缩放的图像看起来更小。
有谁知道发生了什么,为什么我缩放后图像的大小再次改变?
任何线索将不胜感激。
这是我用于调整图像大小的代码:
public static void resizeAndSaveImage(String pathToResize, int newWidth, int newHeight, FileOutputStream output) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(pathToResize), null, options);
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.close();
}
} catch (Exception e) {
// ...
}
Dimension参数传入如下:newWidth = 120,newHeight = 63。