2012-08-14 87 views
2

在设备中使用高分辨率图像时出现问题。Android:问题图像分辨率

imageview a; 
InputStream ims = getAssets().open("sam.png");//sam.png=520*1400 device=320*480 or 480*800 
Drawable d=Drawable.createFromStream(ims, null); 
a.setLayoutParams(new  LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
a.setImageDrawable(d); 

通过上面的代码图像叶顶部和底部的空格下一个内容或者如果我通过给固定像素,其get模糊图像上其尺寸的缩小图像。无论如何要解决这个问题?

回答

0

尝试创建Bitmap,而不是Drawable

Bitmap bmp = BitmapFactory.decodeStream(ims); 
a.setImageBitmap(bmp); 

看起来像Android一样做一些技巧与绘图资源,根据屏幕密度。

+0

是啊!我已经尝试过,但仍然相同 – 2012-08-14 09:27:17

0

希望以下解决方案有帮助。 您可以制作固定大小的imageView,并将该imageView的宽度和高度传递给calculateInSampleSize方法。基于图像大小,它将决定是否降低图像采样。

public Bitmap getBitmap(Context context, final String imagePath) 
{ 
    AssetManager assetManager = context.getAssets(); 
    InputStream inputStream = null; 
    Bitmap bitmap = null; 
    try 
    { 
     inputStream = assetManager.open(imagePath);   

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inScaled = true; 
     options.inJustDecodeBounds = true; 

     // First decode with inJustDecodeBounds=true to check dimensions 
     bitmap = BitmapFactory.decodeStream(inputStream); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight); 

     options.inJustDecodeBounds = false; 

     bitmap = BitmapFactory.decodeStream(inputStream); 
    } 
    catch(Exception exception) 
    { 
     exception.printStackTrace(); 
     bitmap = null; 
    } 

    return bitmap; 
} 


public int calculateInSampleSize(BitmapFactory.Options options, final int requiredWidth, final int requiredHeight) 
{ 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if(height > requiredHeight || width > requiredWidth) 
    { 
     if(width > height) 
     { 
      inSampleSize = Math.round((float)height/(float)requiredHeight); 
     } 
     else 
     { 
      inSampleSize = Math.round((float)width/(float)requiredWidth); 
     } 
    } 

    return inSampleSize; 
} 
+0

是什么意思 final int height = options.outHeight; final int width = options.outWidth;以及如何定义? – 2012-08-14 10:25:39

+0

这些是用于与imageView宽度和高度进行比较的位图的宽度和高度。 – Braj 2012-08-14 10:27:47

+0

你的代码中有一些mistaake,你定义了一些选项,但从来没有使用它们。看看现在存在的官方文档:https://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Vince 2014-11-06 01:20:59