2012-12-01 62 views
2

我需要將頭像添加到網格項目。如何調整圖像大小以適應多個屏幕密度

我想知道如何處理從手機庫中選擇的圖像的大小調整。一旦選定,我想會需要一些調整大小,以適應網格。

但是,我是否需要爲每個屏幕密度存儲調整大小的圖像;存儲一個xhdpi版本並縮小其他設備的尺寸,或以其他方式變得聰明?

原因是,應用程序將此圖像存儲到雲數據庫和其他人可以下載此圖像。他們可能會在不同的設備上看到圖像(因此需要不同的圖像尺寸)。該圖像的管理應如何處理?

回答

2

我希望你找到下面的代碼有用。它將以最小的開銷返回具有所需維度的圖像。我用過這麼多次,像魅力一樣。您可以根據目標設備設置所需的尺寸。縮放會導致圖片模糊,但這不會。

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

} 
+0

謝謝,我會認爲這是一個調整大小的選項。 – HGPB

1
android:scaleType="fitXY" 
android:layout_gravity="center" 

將縮放的圖像和中心它的尺寸大小與以FILL_PARENT一個容器,它應該做到這一點。

+0

有趣的感謝。看起來我可以保存我需要的最大尺寸的圖像,並使用它來管理它的適合性。 – HGPB

0

你可以把你的圖像繪製(無需創建xhdpi,hdpi,mdpi,ldpi ...)(全局圖像)。

然後,您可以爲不同的屏幕尺寸創建4個相同的佈局。您的所有佈局都可以使用可繪製導演中的圖像。所以你可以輕鬆地調整你的圖像。

+0

我不打算用預先完成的圖像填充res文件夾。用戶從畫廊中選擇一張圖片(就像他們自己的照片一樣)。 – HGPB

+0

但它是相同的邏輯。用戶選擇的圖片可以根據您創建的不同佈局調整大小。您可以使用android:scaleType =「fitXY」將圖像放在該圖像上。 – Talha

+0

機器人:scaleType =「fitXY:如果您使用非標準的ImageView大小也可以是有用的,但不同的圖像查看尺寸?圖像比例將下降breaked您可以溶膠這個問題通過裁剪image.android:scaleType="centerCrop」 – Talha

4

做這樣的事情:

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); 
相關問題