Android - 在我以编程方式完成后缩放图像
奇怪的事情发生了——至少我不明白。
我有一张图像(宽:120 像素,高:63 像素),显示在一个 ImageButton 上。我拥有的该图像的唯一变体放置在drawable-hdpi 中(并且没有其他drawable 目录)。 这个图像和每个分辨率(密度)一切都很好,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。
Strange thing happens - at least I don't get it.
I have an image (w: 120px, h: 63px) which is presented on one ImageButton. The only variant of that image that I have is placed in drawable-hdpi (and there is no other drawable directory).
Everything is ok with this image and every resolution (density), Android takes care of it very nicely.
But, trouble is when I take a photo from an album (some very big photo, for example), scale it to dimensions of a button (previously mentioned, w: 120px h: 63px) and set that small image as ImageButton background.
In case when I am testing on emulator with medium density (160) it is ok, but when testing on device with high density button gets resized since scaled image appears smaller.
Does anyone has an idea what is happening and why is size of image changed once more after I scaled it?
Any clue will be highly appreciated.
Here is the code I use for resizing the image:
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 parameters are passed in as follows: newWidth = 120, newHeight = 63.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了实现您想要的效果,您需要使用与密度无关的像素(dp)而不是物理像素。从此处阅读:
为了更详细地解释为什么在一张图像(相册照片)而不是图像资源上发生这种情况,您需要发布用于缩放的代码。
In order to achieve what you want you need to use density-independent pixels (dp) and not physical pixels. Reading from here:
In order to explain in more detail why this is happening with one image (album photo) and not with an image resource, you need to post the code you are using for the scaling.