我想要导入一张照片,然后在ImageView中显示它的“高度”和“宽度”是固定的。首先,我输入BitmapFactory
和options.inSampleSize
的点动以获取位图。其次,我想要获取照片,但我不知道我可以在createScaledBitmap(Bitmap src, int dstWidth, int dstHeight)
和createBitmap(Bitmap source, int x, int y, int width, int height)
中使用哪些照片。
我看到了Android Developer
个文档,但我仍然模糊。我认为createScaledBitmap
已经改变了照片的大小,但createBitmap
可以随意剪切照片。< / p>
public static Bitmap getBitmap(String filePath, String fileName, int reqWidth, int reqHeight) {
if (filePath.length() <= 0 || filePath == null) {
return null;
}
String fullPath = filePath+"/"+fileName;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fullPath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap photoBitmap = BitmapFactory.decodeFile(fullPath, options);
return photoBitmap;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}