//手机屏幕的宽高
int windowWidth = wm.getDefaultDisplay().getWidth();
int windowHeight = wm.getDefaultDisplay().getHeight();
BitmapFactory.Options opts = new Options();
//不去真正解析位图 只是获取图片的宽高
opts.inJustDecodeBounds =true;
BitmapFactory.decodeFile("/sdcard/stop.jpg", opts);
int bitmapHeight = opts.outHeight;
int bitmapWidth = opts.outWidth;
if(bitmapHeight>windowHeight || bitmapWidth>windowWidth){
int scaleX = bitmapWidth/windowWidth;
int scaleY = bitmapHeight/windowHeight;
if(scaleX>scaleY){//水平方向缩放
opts.inSampleSize = scaleX;
} else {//竖直方向缩放
opts.inSampleSize = scaleY;
}
} else {//不缩放
opts.inSampleSize = 1;
}
//让工厂真正的去解析位图
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/stop.jpg", opts);
ib_main_logo.setImageBitmap(bitmap);
原始的处理图片的方式
Bitmap bitmpa1 = BitmapFactory.decodeFile("/sdcard/stop.jpg");
ib_main_logo.setImageBitmap(bitmpa1);
如果图片太大 会抛出OutofMemory