我想导入一张照片,然后在 ImageView 中显示它的“高度”和“宽度”是固定的。首先,我输入 pohot byBitmapFactory并options.inSampleSize获取位图。第二,我想获取照片,但我不知道我可以在createScaledBitmap(Bitmap src, int dstWidth, int dstHeight)和createBitmap(Bitmap source, int x, int y, int width, int height)。
我看过Android Developer文档,但我仍然模糊。我认为createScaledBitmap改变了照片的大小,但是createBitmap可以随心所欲地剪切照片。
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |