【问题标题】:How to resize an image to fit multiple screen densities如何调整图像大小以适应多种屏幕密度
【发布时间】:2012-12-01 13:00:14
【问题描述】:

我需要为网格项目添加头像。

我想知道如何调整从手机图库中选择的图片的大小。一旦选择,我想将需要一些调整大小,以适应网格。

但是,我需要为每个屏幕密度存储一个调整大小的图像吗?为其他设备存储一个 xhdpi 版本并按比例缩小,或者以其他方式聪明?

原因是,应用程序将此图像存储到云数据库中,其他人可以下载此图像。他们可能会在不同的设备上看到图像(因此需要不同的图像尺寸)。这张图片的管理应该如何处理?

【问题讨论】:

    标签: android android-image android-screen-support


    【解决方案1】:

    做这样的事情:

     DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
        Bitmap bitmapOrg = new BitmapDrawable(getResources(), new  ByteArrayInputStream(imageThumbnail)).getBitmap();
    
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
    
        float scaleWidth = metrics.scaledDensity;
        float scaleHeight = metrics.scaledDensity;
    
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
    
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
    

    【讨论】:

      【解决方案2】:

      我希望您发现下面的代码很有用。它将以最小的开销返回具有 reqd 尺寸的图像。我已经用过很多次了,就像魅力一样。您可以根据目标设备设置所需的尺寸。缩放会导致图片模糊,但这不会。

      private Bitmap getBitmap(Uri uri) {                             
          InputStream in = null;
          try {
              final int IMAGE_MAX_SIZE = 200000; // 0.2MP
              in = my_context.getContentResolver().openInputStream(uri);
      
              // Decode image size
              BitmapFactory.Options o = new BitmapFactory.Options();
              o.inJustDecodeBounds = true;                    //request only the dimesion
              BitmapFactory.decodeStream(in, null, o);
              in.close();
      
              int scale = 1;
              while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) > IMAGE_MAX_SIZE) {
                  scale++;
              }
      
              Bitmap b = null;
              in = my_context.getContentResolver().openInputStream(uri);
              if (scale > 1) {
                  scale--;
                  // scale to max possible inSampleSize that still yields an image
                  // larger than target
                  o = new BitmapFactory.Options();
                  o.inSampleSize = scale;
                  b = BitmapFactory.decodeStream(in, null, o);
                  // resize to desired dimensions
                  int height = b.getHeight();
                  int width = b.getWidth();
      
                  double y = Math.sqrt(IMAGE_MAX_SIZE
                          / (((double) width) / height));
                  double x = (y / height) * width;
      
                  Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, (int) y, true);
                  b.recycle();
                  b = scaledBitmap;
                  System.gc();
              } else {
                  b = BitmapFactory.decodeStream(in);
              }
              in.close();
      
              return b;
          } catch (IOException e) {
      
              return null;
          }
      
      }
      

      【讨论】:

      • 谢谢,我会将其视为调整大小的选项。
      【解决方案3】:
      android:scaleType="fitXY"
      android:layout_gravity="center"
      

      将缩放图像并将其居中调整容器大小以填充_parent,它应该这样做。

      【讨论】:

      • 有趣的谢谢。看起来我可以保存我需要的最大尺寸的图像并使用它来管理它的适合度。
      【解决方案4】:

      您可以将图像放入可绘制对象(无需创建 xhdpi、hdpi、mdpi、ldpi...)(全局图像)。

      然后您可以为不同的屏幕尺寸创建 4 个相同的布局。您的所有布局都可以使用可绘制导向器中的图像。因此,您可以轻松调整图像大小。

      【讨论】:

      • 我不打算用预先完成的图像填充 res 文件夹。用户从图库中选择一张图片(比如他们自己的照片)。
      • 不过逻辑是一样的。用户选择的图片,可以在您创建的不同布局中调整大小。您可以使用 android:scaleType="fitXY" 将您的图像放在该图像上。
      • android:scaleType="fitXY : 如果你使用标准的 imageview size 它会很有用。但是不同的 image view size ?? 你的图像比例会被打破。你可以通过裁剪图像来解决这个问题。 android:scaleType="centerCrop"
      • 您会将这些图像存储在设备的什么位置?
      • github.com/nostra13/Android-Universal-Image-Loader你可以看看这个库,很有用
      猜你喜欢
      • 2011-02-03
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多