項目結束,平常忙,現在抽空閑時間總結一下:
大圖片加載時候占用內存很多,圖片占用內存主要和什么有關系呢?
1、主要是與圖片分辨率大小有關
2、本地圖片主要與存放位置有關。
項目中怎么注意呢?
1、以主流的1920*1080為例,存放本地圖片時,如果圖片大,可以使用在線圖片網站壓縮一下,其次,圖片存放位置最好存放到mipmap-xxhdp或者drawable-xxdpi,這個經測試,占用的內存最低,推薦一篇文章寫的比較清晰drawable圖片適配。
2、如果控件大小和加載的圖片Imageview不匹配,可以去設置壓縮一下,壓縮。
方法如下:100,100值可以通過獲取控件設置的大小來設置
imageView.setImageBitmap(??
decodeSampledBitmapFromResource(getResources(),?R.id.myimage,100,?100));??
public?static?Bitmap?decodeSampledBitmapFromResource(Resources?res,?int?resId,int?reqWidth,?int?reqHeight)?{? ? //?第一次解析將inJustDecodeBounds設置為true,來獲取圖片大小??
? final?BitmapFactory.Options?options?=?new?BitmapFactory.Options();??
? ?options.inJustDecodeBounds?=true;??
????BitmapFactory.decodeResource(res,?resId,?options);??
? ?//?調用上面定義的方法計算inSampleSize值??
????options.inSampleSize?=?calculateInSampleSize(options,?reqWidth,?reqHeight);??
? ?//?使用獲取到的inSampleSize值再次解析圖片??
? ? options.inJustDecodeBounds?=false;??
? ?return?BitmapFactory.decodeResource(res,?resId,?options);??
}??
計算縮放因子
public?static?int?calculateInSampleSize(BitmapFactory.Options?options,? int?reqWidth,?int?reqHeight)?{??
//?源圖片的高度和寬度??
final?int?height?=?options.outHeight;??
final?int?width?=?options.outWidth;??
int?inSampleSize?=?1;??
if?(height?>?reqHeight?||?width?>?reqWidth)?{??
//?計算出實際寬高和目標寬高的比率??
final?int?heightRatio?=?Math.round((float)?height?/?(float)?reqHeight);??
final?int?widthRatio?=?Math.round((float)?width?/?(float)?reqWidth);??
//?選擇寬和高中最小的比率作為inSampleSize的值,這樣可以保證最終圖片的寬和高??
//?一定都會大于等于目標的寬和高。??
????????inSampleSize?=?heightRatio?<?widthRatio???heightRatio?:?widthRatio;??
????}??
return?inSampleSize;??
}?