2015-07-21 131 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

不能要求更多,谢谢! –

相关问题