下面列出了android.widget.ImageView#getMaxHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@inheritDoc}
* <br />
* 3) Get <b>maxHeight</b>
*/
@Override
public int getHeight() {
int height = super.getHeight();
if (height <= 0) {
ImageView imageView = (ImageView) viewRef.get();
if (imageView != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
height = imageView.getMaxHeight();
} else {
height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check maxHeight parameter
}
}
}
return height;
}
public static void updateImageView(Context context, ImageView imageView, Uri uri) {
try {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
// Avoid decoding the entire image if the imageView holding this image is smaller.
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, bounds);
int streamWidth = bounds.outWidth;
int streamHeight = bounds.outHeight;
int maxDesiredWidth = imageView.getMaxWidth();
int maxDesiredHeight = imageView.getMaxHeight();
int ratio = Math.max(streamWidth / maxDesiredWidth, streamHeight / maxDesiredHeight);
if (ratio > 1) {
bounds.inSampleSize = ratio;
}
bounds.inJustDecodeBounds = false;
inputStream = context.getContentResolver().openInputStream(uri);
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream, null, bounds));
} catch (FileNotFoundException e) {
Toast.makeText(context, R.string.error_opening_image_file, Toast.LENGTH_SHORT);
}
}
/**
* 获取 ImageView 最大高度
* @param imageView ImageView
* @return view 最大高度
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
public static int getMaxHeight(final ImageView imageView) {
if (imageView != null) {
return imageView.getMaxHeight();
}
return -1;
}
private void setImageToView(){
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_FILE_NAME, 0);
String savedPhotoPath = settings.getString(PHOTO_PATH, null);
//Initialize the image view and display the picture if one exists
imageView = (ImageView) findViewById(R.id.lock_imageView);
Intent intent = getIntent();
String passedPhotoPath = intent.getStringExtra(
MainActivity.EXTRA_FILEPATH);
if (passedPhotoPath != null) {
mCurrentPhotoPath = passedPhotoPath;
} else {
mCurrentPhotoPath = savedPhotoPath;
}
if (mCurrentPhotoPath != null) {
int targetH = imageView.getMaxHeight();
int targetW = imageView.getMaxWidth();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoH = bmOptions.outHeight;
int photoW = bmOptions.outWidth;
// Determine how much to scale down image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,
bmOptions);
imageView.setImageBitmap(imageBitmap);
}
}
private void setImageToView(){
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_FILE_NAME, 0);
String savedPhotoPath = settings.getString(PHOTO_PATH, null);
//Initialize the image view and display the picture if one exists
imageView = (ImageView) findViewById(R.id.lock_imageView);
Intent intent = getIntent();
String passedPhotoPath = intent.getStringExtra(
MainActivity.EXTRA_FILEPATH);
if (passedPhotoPath != null) {
mCurrentPhotoPath = passedPhotoPath;
} else {
mCurrentPhotoPath = savedPhotoPath;
}
if (mCurrentPhotoPath != null) {
int targetH = imageView.getMaxHeight();
int targetW = imageView.getMaxWidth();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoH = bmOptions.outHeight;
int photoW = bmOptions.outWidth;
// Determine how much to scale down image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap imageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath,
bmOptions);
imageView.setImageBitmap(imageBitmap);
}
}