5

我在私人文件上有一张图片。我读取文件,创建可绘制对象,并将其分配给 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");

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

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

看起来它通过密度减少了图像。任何人都可以解释这一点以及如何避免这种情况。

谢谢

4

4 回答 4

2

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

            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);
            }
于 2010-12-17T06:45:03.300 回答
2

我正在处理类似的问题,试试这个:

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-* 目录中选择分辨率/调整图像大小。

于 2011-08-10T13:05:31.940 回答
2

尝试 BitmapDrawable#setTargetDensity:

Bitmap b = BitmapFactory.decodeFile(path)
BitmapDrawable bmd = new BitmapDrawable(b);
bmd.setTargetDensity(getResources().getDisplayMetrics());
于 2013-05-14T07:11:42.063 回答
1

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

于 2010-04-15T22:55:03.533 回答