2015-07-21 129 views

回答

1

是的,它會使用原始大小。您必須調整所有位圖的大小然後將其分配給ImageView,否則內存不足錯誤會出現很多問題。

您還應該計算ImageView的最終大小並調整位圖的大小。

一些代碼讓你去。

private static Bitmap createBitmap(@NonNull String filePath, int width) 
{ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 

    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath , options); 

    // Getting original image properties 
    int imageHeight = options.outHeight; 
    int imageWidth = options.outWidth; 


    int scale  = -1; 
    if (imageWidth < imageHeight) { 
     scale = Math.round(imageHeight/width); 
    } else { 
     scale = Math.round(imageWidth/width); 
    } 
    if (scale <= 0) 
     scale = 1; 

    options.inSampleSize = scale; 
    options.inJustDecodeBounds = false; 

    // Create a resized bitmap 
    Bitmap scaledBitmap = BitmapFactory.decodeFile(filePath , options); 
    return scaledBitmap; 
} 

你還應該考慮:

  • 保持主線程之外的所有位圖操作。
  • 處理併發正確
  • 。利用一些開源的lib的,像這樣的one
+0

不能要求更多,謝謝! –

相關問題