2010-04-15 71 views
4

我在私人文件上有图像。 我读取文件,创建drawable,并将其分配给ImageView。 ImageView具有WRAP_CONTENT,因此大小是自动的。当从文件中读取图像时,android可绘制更改屏幕大小

在屏幕分辨率320x480,图像看起来具有较高分辨率和高密度的480×800像素或480x854(N1,机器人),当图像是例如150×150,我看到了形象的100x100好 但是在屏幕上。

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

这是我的代码:

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

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

fis.close(); 

imageView.setImageDrawable(icon); 

感谢

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

更新:

用下面的代码:

F ileInputStream fis = this.openFileInput(“icon.png”);

icon = Drawable.createFromStream(fis,“icon”);

,如果我再检查图标的大小,机器人认为大小为100×100的时候,确实是150×150。

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

由于

回答

1

设置一些尺寸在与设备无关的像素(或骤降)。

+0

感谢,但对我来说,因为图像可以改变的大小,这就是为什么我有WRAP_CONTENT这不是一个解决方案。 请看有关错误大小的更新。 – 2010-04-16 13:52:05

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); 
      } 
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- *目录。

2

尝试BitmapDrawable#setTargetDensity:

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