【发布时间】:2011-01-25 12:43:10
【问题描述】:
奇怪的事情发生了——至少我不明白。
我有一张图片(宽: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) {
// ...
}
尺寸参数传入如下:newWidth = 120, newHeight = 63。
【问题讨论】:
-
将 api 设置为 1.5,这将阻止 android 调整大小,但意味着您必须这样做。
标签: android background imagebutton image-scaling