Options opt = new Options();
opt.inJustDecodeBounds = true;BitmapFactory.decodeFile("sdcard/dog.jpg", opt);
int imageWidth = opt.outWidth;
int imageHeight = opt.outHeight;
Display dp = getWindowManager().getDefaultDisplay();
int screenWidth = dp.getWidth();
int screenHeight = dp.getHeight();
int scale = 1;
int scaleWidth = imageWidth / screenWidth;
int scaleHeight = imageHeight / screenHeight;
if(scaleWidth >= scaleHeight && scaleWidth >= 1){
scale = scaleWidth;
}
else if(scaleWidth < scaleHeight && scaleHeight >= 1){
scale = scaleHeight;
}
opt.inSampleSize = scale;
opt.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opt);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bm);
本文介绍了一种在Android应用中优化大尺寸图片加载的方法。通过设置BitmapFactory.Options参数inJustDecodeBounds为true来先获取图片尺寸,然后计算合适的inSampleSize值进行采样,最后加载图片到ImageView组件中。
1755

被折叠的 条评论
为什么被折叠?



