1. 程式人生 > >Android圖片處理

Android圖片處理

InputStream is = null;
try {
is = MainActivity.this.getAssets().open(imgurl);
} catch (IOException e) {
e.printStackTrace();
}  
//Bitmap bitmap = BitmapFactory.decodeStream(is);  
//viewHolder.getIv_img().setImageBitmap(bitmap);

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bitmap2 = BitmapFactory.decodeStream(is, null, opts);
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWdith = dm.widthPixels;
int screenHeight = dm.heightPixels;

int scale = 1;
int scaleWidth = imgWidth/screenWdith;
int scaleHeight = imgHeight/screenHeight;
if(scaleWidth>scaleHeight&&imgWidth>screenWdith){
scale = scaleWidth;
}else if(scaleHeight>scaleWidth&&imgHeight>screenHeight){
scale = scaleHeight;
}
opts.inSampleSize = scale;
opts.inJustDecodeBounds = false;
Bitmap bitmap3 = BitmapFactory.decodeStream(is, null, opts);
viewHolder.getIv_img().setImageBitmap(bitmap3);