我正在努力在我的两个活动中展示适当大小的图像。 CatalogActivity中的图像看起来像是正确缩小然后裁剪,而EditorActivity中的图像是完整的并且要小得多。 唯一的区别是目录中的图像来自图像资源,而编辑器图像来自画廊,所以来自Uri。
你能告诉我为什么这个差异以及如何使EditorActivity的图像与另一个图像相同?
来自CatalogActivity:
MiningField#setUsageType(MiningField$UsageType)
来自EditorActivity:
// Make the sample image smaller so it doesn't take too much space in the memory
Bitmap sourdoughBitmap = ProductsEntry.decodeSampledBitmapFromResource(getResources(), R.drawable.sourdough_picture, 72, 72);
来自ProductsEntry:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == RESULT_LOAD_PICTURE && resultCode == RESULT_OK && null != intent) {
Uri selectedPictureUri = intent.getData();
// Show the selected picture in an ImageView in the editor
try {
// Scale the bitmap received from the uri so it fits in the small ImageView
scaledPictureBitmap = ProductsEntry.decodeSampledBitmapFromUri(this, selectedPictureUri, 72, 72);
// Hide the gray picture placeholder
mImageView.setBackgroundResource(0);
// Show the scaled bitmap in the ImageView
mImageView.setImageBitmap(scaledPictureBitmap);
// The user has chosen a picture and we can change
// the text of the button to say "Change picture"
mAddPictureButton.setText(R.string.edit_product_change_photo);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
来自activity_editor.xml:
/**
* Helps create a smaller bitmap image from resource
* @param resources - a resources object
* @param resourceId - the id of the image in the drawable folder
* @param requiredWidth - the width that we want for the final image
* @param requiredHeight - the height that we want for the final image
* @return the decoded Bitmap
*/
public static Bitmap decodeSampledBitmapFromResource(Resources resources, int resourceId,
int requiredWidth, int requiredHeight){
// First decode with inJustDecodeBounds = true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resourceId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
// Decode bitmap with inJustDecodeBounds = false
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resourceId, options);
}
/**
* Helps create a smaller bitmap image from a uri
* @param context - a context object
* @param uri - the uri of the image
* @param requiredWidth - the width that we want for the final image
* @param requiredHeight - the height that we want for the final image
* @return the decoded Bitmap
*/
public static Bitmap decodeSampledBitmapFromUri(Context context, Uri uri, int requiredWidth, int requiredHeight)
throws FileNotFoundException {
// First decode with inJustDecodeBounds = true, only to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
// Decode bitmap with inJustDecodeBounds = false
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
}
/**
* Calculate a sample size value that is a power of 2 based on a target width and height
* @param options is used to pass options to the BitmapFactory
* @param requiredWidth is the width that we want for the final image
* @param requiredHeight is the height that we want for the final image
* @return by how much to scale down the image
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int requiredWidth, int requiredHeight) {
// Raw height and width of the image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > requiredHeight || width > requiredWidth) {
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 required height and width
while ((halfHeight / inSampleSize) >= requiredHeight
&& (halfWidth / inSampleSize) >= requiredWidth){
inSampleSize *= 2;
}
}
return inSampleSize;
}
链接到存储库here。