从文件读取图像时,androiddrawable改变屏幕上的大小

发布于 2024-08-29 04:43:30 字数 780 浏览 6 评论 0原文

我的私人文件中有一张图片。 我读取文件,创建可绘制对象,并将其分配给 ImageView。 ImageView 有 WRAP_CONTENT 所以大小是自动的。

在 320x480 屏幕上,图像看起来不错 但在分辨率更高、密度更高的 480x800 或 480x854(N1、droid)屏幕上,当图像为 150x150 时,我看到的图像为 100x100。

当然,这与密度有关,但不知道该如何解决这个问题。

这是我的代码:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "icon");

fis.close();

imageView.setImageDrawable(icon);

谢谢

============================================== ====================

更新:

使用以下代码:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "图标");

如果我然后检查图标的大小,android 认为大小是 100x100,而实际上是 150x150。

看起来它降低了图像的密度。 任何人都可以解释一下这一点以及如何避免这种情况。

谢谢

I have an image on a private file.
I read the file, create the drawable, and assign it to an ImageView.
The ImageView has WRAP_CONTENT so the size is automatic.

On 320x480 screens, the image looks good
But on screens with more resolution and high density 480x800 or 480x854 (N1, droid) , when the image is for example 150x150, I see the image as 100x100.

Of course it has something to do with the density but not sure how should I resolve this.

This is my code:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "icon");

fis.close();

imageView.setImageDrawable(icon);

thanks

================================================================

update:

with the following code:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "icon");

if I then inspect the size of the icon, android thinks the size is 100x100, when really is 150x150.

Looks like its reducing the image by the density.
Can anybody explain this and how to avoid this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

羁客 2024-09-05 04:43:30

我正在研究类似的问题,试试这个:

TypedValue typedValue = new TypedValue();
//density none divides by the density, density default keeps the original size
typedValue.density = TypedValue.DENSITY_DEFAULT;
Drawable d = Drawable.createFromResourceStream(null, typedValue, fis, "icon");

我仍在研究如何开发一个通用解决方案来从通用流而不是drawable-*目录中选择分辨率/调整图像大小。

I am working on a similar issue, try this:

TypedValue typedValue = new TypedValue();
//density none divides by the density, density default keeps the original size
typedValue.density = TypedValue.DENSITY_DEFAULT;
Drawable d = Drawable.createFromResourceStream(null, typedValue, fis, "icon");

I am still working through how to develop a general solution to pick resolution/resize images from a generic stream instead of the drawable-* directories.

£烟消云散 2024-09-05 04:43:30

将图像重新缩放至所需尺寸:

            d = Drawable.createFromPath(filePath);
            if (d != null) {
                Bitmap bitmapOrg = ((BitmapDrawable) d).getBitmap();
                int width = bitmapOrg.getWidth();
                int height = bitmapOrg.getHeight();
                int newWidth = 170;
                int newHeight = 170;
                // calculate the scale
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                // create a matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                width, height, matrix, true);
                // make a Drawable from Bitmap to allow to set the BitMap
                d = new BitmapDrawable(resizedBitmap);
            }

Rescale the image to desired dimension:

            d = Drawable.createFromPath(filePath);
            if (d != null) {
                Bitmap bitmapOrg = ((BitmapDrawable) d).getBitmap();
                int width = bitmapOrg.getWidth();
                int height = bitmapOrg.getHeight();
                int newWidth = 170;
                int newHeight = 170;
                // calculate the scale
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                // create a matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                width, height, matrix, true);
                // make a Drawable from Bitmap to allow to set the BitMap
                d = new BitmapDrawable(resizedBitmap);
            }
小巷里的女流氓 2024-09-05 04:43:30

尝试 BitmapDrawable#setTargetDensity:

Bitmap b = BitmapFactory.decodeFile(path)
BitmapDrawable bmd = new BitmapDrawable(b);
bmd.setTargetDensity(getResources().getDisplayMetrics());

Try BitmapDrawable#setTargetDensity:

Bitmap b = BitmapFactory.decodeFile(path)
BitmapDrawable bmd = new BitmapDrawable(b);
bmd.setTargetDensity(getResources().getDisplayMetrics());
思慕 2024-09-05 04:43:30

以设备独立像素(或 DIP)设置一些尺寸。

Set some dimensions in device independent pixels (or DIPs).

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