Skip to content

Commit

Permalink
[Android] Fix image not growing (microsoft#3415)
Browse files Browse the repository at this point in the history
* Fix images not growing in Android

* Fix height behaviour for images and add tests

* Add test results
  • Loading branch information
almedina-ms authored Sep 26, 2019
1 parent 66d7a73 commit cfbb671
Showing 1 changed file with 66 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,57 @@ protected void renderBitmap(Bitmap bitmap)
private static int getImageSizeLimit(Context context, ImageSize imageSize, ImageSizesConfig imageSizesConfig) {
int imageSizeLimit = context.getResources().getDisplayMetrics().widthPixels;

if (imageSize == ImageSize.Small) {
if (imageSize == ImageSize.Small)
{
imageSizeLimit = Util.dpToPixels(context, imageSizesConfig.getSmallSize());
} else if (imageSize == ImageSize.Medium) {
}
else if (imageSize == ImageSize.Medium)
{
imageSizeLimit = Util.dpToPixels(context, imageSizesConfig.getMediumSize());
} else if (imageSize == ImageSize.Large) {
}
else if (imageSize == ImageSize.Large)
{
imageSizeLimit = Util.dpToPixels(context, imageSizesConfig.getLargeSize());
}

return imageSizeLimit;
}

private static void setImageSize(Context context, ImageView imageView, ImageSize imageSize, ImageSizesConfig imageSizesConfig) {
private static void setImageSize(Context context, ImageView imageView, ImageSize imageSize, ImageSizesConfig imageSizesConfig, boolean isInImageSet) {
imageView.setScaleType(ImageView.ScaleType.CENTER);
if (imageSize == ImageSize.Stretch) {
//ImageView must match parent for stretch to work
imageView.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
if (imageSize == ImageSize.Stretch)
{
if (!isInImageSet)
{
imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
else if ((imageSize == ImageSize.Small) || (imageSize == ImageSize.Medium) || (imageSize == ImageSize.Large))
{
int size = getImageSizeLimit(context, imageSize, imageSizesConfig);

if (imageView.getLayoutParams() == null)
{
imageView.setLayoutParams(new LinearLayout.LayoutParams(size, size));
}
else
{
imageView.getLayoutParams().height = size;
imageView.getLayoutParams().width = size;
}

imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
} else if (imageSize == ImageSize.Small) {
imageView.setMaxWidth(Util.dpToPixels(context, imageSizesConfig.getSmallSize()));
} else if (imageSize == ImageSize.Medium) {
imageView.setMaxWidth(Util.dpToPixels(context, imageSizesConfig.getMediumSize()));
} else if (imageSize == ImageSize.Large) {
imageView.setMaxWidth(Util.dpToPixels(context, imageSizesConfig.getLargeSize()));
} else if (imageSize != ImageSize.Auto && imageSize != ImageSize.None){
}
else if (imageSize != ImageSize.Auto && imageSize != ImageSize.None)
{
throw new IllegalArgumentException("Unknown image size: " + imageSize.toString());
}

imageView.setAdjustViewBounds(true);
}

private void setImageSize(Context context, ImageView imageView, Image image, HostConfig hostConfig)
private void setImageSize(Context context, ImageView imageView, Image image, HostConfig hostConfig, boolean isInImageSet)
{
long pixelWidth = image.GetPixelWidth();
long pixelHeight = image.GetPixelHeight();
Expand Down Expand Up @@ -185,7 +205,7 @@ private void setImageSize(Context context, ImageView imageView, Image image, Hos
}
else
{
setImageSize(context, imageView, image.GetImageSize(), hostConfig.GetImageSizes());
setImageSize(context, imageView, image.GetImageSize(), hostConfig.GetImageSizes(), isInImageSet);
}
}

Expand Down Expand Up @@ -217,52 +237,53 @@ private int getBackgroundColorFromHexCode(String hexColorCode)
return backgroundColor;
}

private LinearLayout.LayoutParams setImageHorizontalAlignment(LinearLayout.LayoutParams layoutParams, Image image)
private int getImageHorizontalAlignment(Image image)
{
HorizontalAlignment horizontalAlignment = image.GetHorizontalAlignment();
if (horizontalAlignment == HorizontalAlignment.Right)
int gravity = Gravity.LEFT;

if (horizontalAlignment == HorizontalAlignment.Center)
{
layoutParams.gravity = Gravity.RIGHT;
gravity = Gravity.CENTER_HORIZONTAL;
}
else if (horizontalAlignment == HorizontalAlignment.Center)
else if (horizontalAlignment == HorizontalAlignment.Right)
{
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
gravity = Gravity.RIGHT;
}

return layoutParams;
return gravity;
}

private View setImageHeightAndHorizontalAlignment(Context context, boolean isInImageSet, ImageView imageView, Image image, TagContent tagContent)
{
LinearLayout.LayoutParams layoutParams;
int height = LinearLayout.LayoutParams.WRAP_CONTENT, width = LinearLayout.LayoutParams.WRAP_CONTENT, weight = 0;

// The expected behavior for the image must be:
// If the image has stretch size:
// width grows as big as the parent but only uses as much vertical space as needed
// If the image has stretch size and also stretches in height:
// width grows as big as the parent and uses as much leftover vertical space as there is
// If the image has a fixed or auto size:
// width is limited to image size or defined size and only uses as much vertical space as needed
// If the image has a fixed or auto size and also stretched in height:
// width is limited to image size or defined size and uses as much leftover vertical space as there is
if (image.GetImageSize() == ImageSize.Stretch)
{
// ImageView must match parent for stretch to work
if (image.GetHeight() == HeightType.Stretch)
{
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
}
else
{
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
width = LinearLayout.LayoutParams.MATCH_PARENT;
}
else

if (image.GetHeight() == HeightType.Stretch)
{
if (image.GetHeight() == HeightType.Stretch)
{
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
}
else
{
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
height = LinearLayout.LayoutParams.MATCH_PARENT;
weight = 1;
}

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height, weight);

// Set horizontal alignment for the image
layoutParams = setImageHorizontalAlignment(layoutParams, image);
layoutParams.gravity = getImageHorizontalAlignment(image);

// If the image is part of an imageSet or have height auto, we set the layout parameter to
// If the image is part of an imageSet or has height auto, we set the layout parameter to
// it and return just the view, otherwise we have to create a layout to grow in height and
// contain the image avoiding it to grow larger and making the background color to stretch
if (isInImageSet || image.GetHeight() != HeightType.Stretch)
Expand Down Expand Up @@ -330,11 +351,11 @@ else if ((image = Image.dynamic_cast(baseCardElement)) == null)

imageLoaderAsync.execute(image.GetUrl());

setImageSize(context, imageView, image, hostConfig);

TagContent tagContent = new TagContent(image, separator, viewGroup);

View imageContainer = setImageHeightAndHorizontalAlignment(context, isInImageSet, imageView, image, tagContent);
setImageSize(context, imageView, image, hostConfig, isInImageSet);

viewGroup.addView(imageContainer);
imageView.setTag(tagContent);
setVisibility(baseCardElement.GetIsVisible(), imageView);
Expand Down

0 comments on commit cfbb671

Please sign in to comment.