Android圖片處理
阿新 • • 發佈:2019-02-07
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);
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);