Android - 在我以编程方式完成后缩放图像

发布于 2024-10-13 15:32:57 字数 1354 浏览 5 评论 0原文

奇怪的事情发生了——至少我不明白。

我有一张图像(宽: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-10-20 15:32:57

为了实现您想要的效果,您需要使用与密度无关的像素(dp)而不是物理像素。从此处阅读:

与密度无关的像素是
相当于一个物理像素
160 dpi 屏幕,基线密度
平台假设(如描述
本文档稍后)。在运行时,
该平台透明地处理任何
所需 dp 单位的缩放,基于
屏幕的实际密度
使用。 dp单位换算为
屏幕像素很简单:像素= dps
*(密度/160)。例如,在 240 dpi 屏幕上,1 dp 等于 1.5
物理像素。使用 dp 单位
定义你的应用程序的 UI 是高度
建议,作为确保
在不同的平台上正确显示您的 UI
屏幕。

为了更详细地解释为什么在一张图像(相册照片)而不是图像资源上发生这种情况,您需要发布用于缩放的代码。

In order to achieve what you want you need to use density-independent pixels (dp) and not physical pixels. Reading from here:

The density-independent pixel is
equivalent to one physical pixel on a
160 dpi screen, the baseline density
assumed by the platform (as described
later in this document). At run time,
the platform transparently handles any
scaling of the dp units needed, based
on the actual density of the screen in
use. The conversion of dp units to
screen pixels is simple: pixels = dps
* (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5
physical pixels. Using dp units to
define your application's UI is highly
recommended, as a way of ensuring
proper display of your UI on different
screens.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文