【问题标题】:Android:Issue Image resolutionAndroid:问题图像分辨率
【发布时间】:2012-08-14 09:15:03
【问题描述】:

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

 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);

通过使用上面的代码图像在下一个内容的顶部和底部留下空间,或者如果我通过提供固定像素来缩小图像,它会在其大小上获得模糊图像。无论如何要解决这个问题?

【问题讨论】:

    标签: java android imageview resolution


    【解决方案1】:

    尝试创建Bitmap 而不是Drawable

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

    看起来 Android 使用 drawables 做了一些技巧,具体取决于屏幕密度。

    【讨论】:

      【解决方案2】:

      希望以下解决方案有所帮助。 您可以将 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;
      }
      

      【讨论】:

      • 最终 int 高度是什么意思 = options.outHeight;最终 int 宽度 = options.outWidth;以及如何定义?
      • 这些是位图的宽度和高度,用于与 imageView 的宽度和高度进行比较。
      • 您的代码中有一些错误,您定义了一些选项,但从不使用它们。查看现在存在的官方文档:developer.android.com/training/displaying-bitmaps/…
      • @Vince 选项被使用。看清楚代码。
      • 我的意思是解码流。我的代码看起来像使用 BitMapFactory.decodeStream(inputStream, rect, options)。在您的代码中,您读取和写入选项,但从不将它们应用于任何东西(至少我看不到在哪里)。
      猜你喜欢
      • 2013-05-08
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多