Skip to content

Commit

Permalink
Merge pull request #4738 from salachi/TIMOB-14395-ImageViewScaling
Browse files Browse the repository at this point in the history
TIMOB-14395-if height or width not defined, set those keeping aspect rat...
  • Loading branch information
hieupham007 committed Oct 13, 2013
2 parents 6f9dc3d + 2dfb508 commit 88f6c0f
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,32 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
// Log.i(LCAT, "w: " + w + " wm: " + wm + " h: " + h + " hm: " + hm);
// }

// If height or width is not defined, we need to set the height/width properly
// so that it doesn't get the content height/width
if (!viewWidthDefined || !viewHeightDefined) {
Drawable d = imageView.getDrawable();
float aspectRatio = 1;
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);

if (d != null) {
int ih = d.getIntrinsicHeight();
int iw = d.getIntrinsicWidth();
if (ih != 0 && iw != 0) {
aspectRatio = ih / iw;
}
}

if (viewWidthDefined) {
maxWidth = w;
maxHeight = Math.round(w * aspectRatio);
}
if (viewHeightDefined) {
maxHeight = h;
maxWidth = Math.round(h / aspectRatio);
}
}

// TODO padding and margins

measureChild(imageView, widthMeasureSpec, heightMeasureSpec);
Expand Down

0 comments on commit 88f6c0f

Please sign in to comment.