coding过程中会遇到将本地的图片的物理路径转化为Bitmap,可使用下面的方法:
/**
* @param path:图片的物理路径
* @param display_width:设置图片的宽
* @param display_height:设置图片的高
* @return
* @throws Exception
*/
private Bitmap decodeBitmap(String path,float display_width,float display_height) throws Exception {
if (Float.compare(display_width,0.0f) == 0 || Float.compare(display_width,0.0f) == 0) {
throw new Exception("图片的宽高均不能为0!");
}
BitmapFactory.Options op = new BitmapFactory.Options();
//inJustDecodeBounds
//If set to true, the decoder will return null (no bitmap), but the out…
op.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
//获取比例大小
int wRatio = (int)Math.ceil(op.outWidth/display_width);
int hRatio = (int)Math.ceil(op.outHeight/display_height);
//如果超出指定大小,则缩小相应的比例
if(wRatio > 1 && hRatio > 1){
if(wRatio > hRatio){
op.inSampleSize = wRatio;
}else{
op.inSampleSize = hRatio;
}
}
op.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(path, op);
return bmp;
}