diff options
Diffstat (limited to 'core/java/android/widget/ImageView.java')
-rw-r--r-- | core/java/android/widget/ImageView.java | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/core/java/android/widget/ImageView.java b/core/java/android/widget/ImageView.java index 26c801f..8c710ce 100644 --- a/core/java/android/widget/ImageView.java +++ b/core/java/android/widget/ImageView.java @@ -30,6 +30,7 @@ import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; import android.util.Log; @@ -40,6 +41,9 @@ import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; import android.widget.RemoteViews.RemoteView; +import java.io.IOException; +import java.io.InputStream; + /** * Displays an arbitrary image, such as an icon. The ImageView class * can load images from various sources (such as resources or content @@ -90,6 +94,9 @@ public class ImageView extends View { private int mBaseline = -1; private boolean mBaselineAlignBottom = false; + // AdjustViewBounds behavior will be in compatibility mode for older apps. + private boolean mAdjustViewBoundsCompat = false; + private static final ScaleType[] sScaleTypeArray = { ScaleType.MATRIX, ScaleType.FIT_XY, @@ -164,6 +171,8 @@ public class ImageView extends View { private void initImageView() { mMatrix = new Matrix(); mScaleType = ScaleType.FIT_CENTER; + mAdjustViewBoundsCompat = mContext.getApplicationInfo().targetSdkVersion <= + Build.VERSION_CODES.JELLY_BEAN_MR1; } @Override @@ -225,8 +234,15 @@ public class ImageView extends View { /** * Set this to true if you want the ImageView to adjust its bounds * to preserve the aspect ratio of its drawable. + * + * <p><strong>Note:</strong> If the application targets API level 17 or lower, + * adjustViewBounds will allow the drawable to shrink the view bounds, but not grow + * to fill available measured space in all cases. This is for compatibility with + * legacy {@link android.view.View.MeasureSpec MeasureSpec} and + * {@link android.widget.RelativeLayout RelativeLayout} behavior.</p> + * * @param adjustViewBounds Whether to adjust the bounds of this view - * to presrve the original aspect ratio of the drawable + * to preserve the original aspect ratio of the drawable. * * @see #getAdjustViewBounds() * @@ -635,20 +651,27 @@ public class ImageView extends View { } } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme)) { + InputStream stream = null; try { - d = Drawable.createFromStream( - mContext.getContentResolver().openInputStream(mUri), - null); + stream = mContext.getContentResolver().openInputStream(mUri); + d = Drawable.createFromStream(stream, null); } catch (Exception e) { Log.w("ImageView", "Unable to open content: " + mUri, e); + } finally { + if (stream != null) { + try { + stream.close(); + } catch (IOException e) { + Log.w("ImageView", "Unable to close content: " + mUri, e); + } + } } - } else { + } else { d = Drawable.createFromPath(mUri.toString()); } if (d == null) { - System.out.println("resolveUri failed on bad bitmap uri: " - + mUri); + System.out.println("resolveUri failed on bad bitmap uri: " + mUri); // Don't try again. mUri = null; } @@ -792,6 +815,12 @@ public class ImageView extends View { if (resizeWidth) { int newWidth = (int)(desiredAspect * (heightSize - ptop - pbottom)) + pleft + pright; + + // Allow the width to outgrow its original estimate if height is fixed. + if (!resizeHeight && !mAdjustViewBoundsCompat) { + widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec); + } + if (newWidth <= widthSize) { widthSize = newWidth; done = true; @@ -802,6 +831,13 @@ public class ImageView extends View { if (!done && resizeHeight) { int newHeight = (int)((widthSize - pleft - pright) / desiredAspect) + ptop + pbottom; + + // Allow the height to outgrow its original estimate if width is fixed. + if (!resizeWidth && !mAdjustViewBoundsCompat) { + heightSize = resolveAdjustedSize(newHeight, mMaxHeight, + heightMeasureSpec); + } + if (newHeight <= heightSize) { heightSize = newHeight; } |