/**
* 得到一定高度的图片
*
* @param height
* @param imagePath
* @return
*/
public Bitmap getImageByHeight(float height, String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / height);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
return bitmap;
}
/**
* 得到一定宽度的图片
*
* @param width
* @param imagePath
* @return
*/
public Bitmap getImageByWidth(float width, String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outWidth / width);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
return bitmap;
}
/**
* 得到一定宽度或高度的图片
* @param width 宽度
* @param height 高度
* @param srcPath 图片路径
* @return Bitmap
*/
public static Bitmap getBitmap(String srcPath,float width,float height) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > width) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / width);
} else if (w < h && h > height) {//如果高度高的话根据高度固定大小缩放
be = (int) (newOpts.outHeight / height);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return bitmap;
}