我想从图库中加载图像作为位图并在自定义视图(不是图像视图)上显示它。这是加载位图的代码。
private Bitmap loadBitmap(String filePath, int orientation, int width, int height) {
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
int scale = 1;
if ((options.outWidth > width) || (options.outHeight > height)) {
if (options.outWidth < options.outHeight) {
scale = Math.round((float) options.outWidth / width);
} else {
scale = Math.round((float) options.outHeight/ height);
}
}
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(filePath, options);
if (orientation > 0) {
// rotate the image w.r.to orientation
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0,width,height, matrix, true);
}
} catch (Exception e) {
bitmap = null;
}
return bitmap;
}
现在我的问题是画布没有完全显示位图。只有一部分。我怎样才能用屏幕大小调整图像(不失去清晰度,不拉伸)?
提前致谢