图片太大导致 imageView无法显示
项目中遇到预置大图,在imageView加载时显示不出来
从网络中收集资料,明白了原来imageView 在把图片填充到控件中,是通过分辨率(例如:720*1280)把像素点
填充到画布上 ;也就是说,不管你把图片从20M 压缩到2M,在画布上只会根据你的实际分辨率来绘制bitmap.
你可以把你压缩过的图片放到PS上看一下实际绘制会多大
以下是我通过判断bitmap实际宽度,来重新 createScaledBitmap 让图片可以正常加载。
private Bitmap getScaleBitmap(String wallpaperPath) {
Bitmap bm = BitmapFactory.decodeFile(wallpaperPath);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth=dm.widthPixels;
/**
* 图片的宽度 (用于判断当前图判是否过大,避免OOM使得imageview无法显示)
* 适用于 720P,其他分辨率需要重新修改
*/
int mFixedWidth = 2870;
//图片的高度 (用于客户预置特别大图,重新处理bitmap)
int mFixedHeight = 1920;
if(bm.getWidth()<=screenWidth){
return bm;
}else{
//Bitmap bmp=Bitmap.createScaledBitmap(bm, screenWidth, bm.getHeight()*screenWidth/bm.getWidth(), true);
if (bm.getWidth()<= mFixedWidth) {
return bm;
}
Bitmap bmp=Bitmap.createScaledBitmap(bm, bm.getWidth()*mFixedHeight/bm.getHeight(), mFixedHeight, true);
return bmp;
}
}
以上方法优缺点:
优点: 可以让所有图片顺利的加载到imageview中
缺点: 重新对图片进行了缩略,损失了部分分辨率