Skip to content

Commit 57a4720

Browse files
committed
Image sizing improvements
1 parent 68886eb commit 57a4720

File tree

4 files changed

+40
-57
lines changed

4 files changed

+40
-57
lines changed

libraries/sdk/src/main/java/com/fastcomments/sdk/FeedPostsAdapter.java

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ private int calculateImageHeight(FeedPostMediaItemAsset asset, int displayWidth)
281281
// Calculate height based on aspect ratio and display width
282282
int calculatedHeight = (int) (displayWidth / aspectRatio);
283283

284-
// Set minimum and maximum bounds to prevent extreme values
284+
// Set minimum bound to prevent extremely small images
285285
int minHeight = context.getResources().getDimensionPixelSize(R.dimen.feed_image_half_height);
286-
int maxHeight = context.getResources().getDimensionPixelSize(R.dimen.feed_image_height) * 2;
287286

288-
// Ensure the height is within reasonable bounds
289-
return Math.max(minHeight, Math.min(calculatedHeight, maxHeight));
287+
// Ensure the height is at least the minimum height
288+
// but allow it to be as tall as needed by the aspect ratio
289+
return Math.max(minHeight, calculatedHeight);
290290
}
291291

292292
enum FeedPostType {
@@ -486,21 +486,12 @@ private void bindSingleImagePost(FeedPost post) {
486486
if (bestSizeAsset != null && bestSizeAsset.getSrc() != null) {
487487
mediaContainer.setVisibility(View.VISIBLE);
488488

489-
// Get the screen width for calculating the appropriate height
490-
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
491-
492-
// Calculate the image height based on the aspect ratio
493-
int calculatedHeight = calculateImageHeight(bestSizeAsset, screenWidth);
489+
// Don't set fixed height - let the image determine its own size
490+
// with adjustViewBounds
494491

495-
// Set the calculated height before loading the image
496-
// This prevents the layout from jumping when the image loads
497-
mediaImageView.getLayoutParams().height = calculatedHeight;
498-
mediaImageView.requestLayout();
499-
500-
// Load image with exact calculated dimensions to prevent scaling issues
492+
// Load image without overriding dimensions
501493
Glide.with(context)
502494
.load(bestSizeAsset.getSrc())
503-
.override(screenWidth, calculatedHeight)
504495
.transition(DrawableTransitionOptions.withCrossFade(300))
505496
.error(R.drawable.image_placeholder)
506497
.into(mediaImageView);
@@ -662,15 +653,15 @@ private void setupImageGrid(List<FeedPostMediaItem> mediaItems) {
662653
params.height = GridLayout.LayoutParams.MATCH_PARENT;
663654
imageGridLayout.addView(imageView, params);
664655
} else if (count == 2) {
665-
// Two images side by side (only handles 1-2 images now)
656+
// Two images stacked vertically
666657
for (int i = 0; i < count; i++) {
667658
ImageView imageView = createImageView(mediaItems.get(i));
668659
GridLayout.LayoutParams params = new GridLayout.LayoutParams(
669-
GridLayout.spec(0, 2, 1f),
670-
GridLayout.spec(i, 1, 1f));
671-
params.width = 0;
672-
params.height = GridLayout.LayoutParams.MATCH_PARENT;
673-
params.setMargins(i > 0 ? 2 : 0, 0, i > 0 ? 0 : 2, 0);
660+
GridLayout.spec(i, 1, 1f), // i'th row, 1 row, 1f weight
661+
GridLayout.spec(0, 1, 1f)); // 0th column, 1 column, 1f weight
662+
params.width = GridLayout.LayoutParams.MATCH_PARENT;
663+
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
664+
params.setMargins(0, i > 0 ? 4 : 0, 0, i < count - 1 ? 4 : 0); // Vertical margins between images
674665
imageGridLayout.addView(imageView, params);
675666
}
676667
}
@@ -785,45 +776,46 @@ private void loadImageIntoView(FeedPostMediaItem mediaItem, ImageView imageView)
785776
*/
786777
private ImageView createImageView(FeedPostMediaItem mediaItem) {
787778
ImageView imageView = new ImageView(context);
788-
imageView.setBackgroundColor(context.getResources().getColor(android.R.color.darker_gray, null));
789779

790-
// Calculate initial image height based on screen width and estimated grid column width
780+
// Set up ImageView properties similar to single image view
781+
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
782+
imageView.setAdjustViewBounds(true);
783+
784+
// Use full screen width for stacked images
791785
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
792-
int estimatedWidth = screenWidth / 2; // Grid images usually take about half the screen width
793-
int initialHeight = getDefaultImageHeight(); // Default fallback height
794786

795787
// Load image using Glide if media item has sizes
796788
if (mediaItem.getSizes() != null && !mediaItem.getSizes().isEmpty()) {
797789
FeedPostMediaItemAsset bestSizeAsset = selectBestImageSize(mediaItem.getSizes());
798790

799791
if (bestSizeAsset != null && bestSizeAsset.getSrc() != null) {
800-
// Calculate height based on aspect ratio before loading
801-
if (bestSizeAsset.getW() != null && bestSizeAsset.getH() != null) {
802-
initialHeight = calculateImageHeight(bestSizeAsset, estimatedWidth);
803-
}
804-
805-
// Set the calculated height
792+
// Set up layout parameters for wrap_content height
806793
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
807-
ViewGroup.LayoutParams.MATCH_PARENT, initialHeight);
794+
ViewGroup.LayoutParams.MATCH_PARENT,
795+
ViewGroup.LayoutParams.WRAP_CONTENT);
808796
imageView.setLayoutParams(layoutParams);
809797

798+
// Let Glide load the image with natural aspect ratio
810799
Glide.with(context)
811800
.load(bestSizeAsset.getSrc())
812-
.override(estimatedWidth, initialHeight)
813801
.transition(DrawableTransitionOptions.withCrossFade())
814802
.error(R.drawable.image_placeholder)
815803
.into(imageView);
816804
} else {
817-
// Set default height if no valid asset
805+
// Set fallback if no valid asset
818806
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
819-
ViewGroup.LayoutParams.MATCH_PARENT, initialHeight);
807+
ViewGroup.LayoutParams.MATCH_PARENT,
808+
ViewGroup.LayoutParams.WRAP_CONTENT);
820809
imageView.setLayoutParams(layoutParams);
810+
imageView.setImageResource(R.drawable.image_placeholder);
821811
}
822812
} else {
823-
// Set default height if no sizes
813+
// Set fallback if no sizes
824814
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
825-
ViewGroup.LayoutParams.MATCH_PARENT, initialHeight);
815+
ViewGroup.LayoutParams.MATCH_PARENT,
816+
ViewGroup.LayoutParams.WRAP_CONTENT);
826817
imageView.setLayoutParams(layoutParams);
818+
imageView.setImageResource(R.drawable.image_placeholder);
827819
}
828820

829821
// Set click listener
@@ -874,21 +866,10 @@ private void bindTaskPost(FeedPost post) {
874866

875867
if (bestSizeAsset != null && bestSizeAsset.getSrc() != null) {
876868
mediaContainer.setVisibility(View.VISIBLE);
877-
// Get the screen width for calculating the appropriate height
878-
int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
879-
880-
// Calculate the image height based on the aspect ratio
881-
int calculatedHeight = calculateImageHeight(bestSizeAsset, screenWidth);
882-
883-
// Set the calculated height before loading the image
884-
// This prevents the layout from jumping when the image loads
885-
mediaImageView.getLayoutParams().height = calculatedHeight;
886-
mediaImageView.requestLayout();
887869

888-
// Load image with exact calculated dimensions to prevent scaling issues
870+
// Load image without overriding dimensions
889871
Glide.with(context)
890872
.load(bestSizeAsset.getSrc())
891-
.override(screenWidth, calculatedHeight)
892873
.transition(DrawableTransitionOptions.withCrossFade(300))
893874
.error(R.drawable.image_placeholder)
894875
.into(mediaImageView);

libraries/sdk/src/main/res/layout/feed_post_item_multi_image.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@
8484
<GridLayout
8585
android:id="@+id/imageGridLayout"
8686
android:layout_width="match_parent"
87-
android:layout_height="300dp"
88-
android:columnCount="2"
87+
android:layout_height="wrap_content"
88+
android:columnCount="1"
8989
android:rowCount="2"
9090
android:visibility="gone"
91-
android:padding="1dp" />
91+
android:padding="0dp" />
9292

9393
<!-- Linear layout for exactly 3 images -->
9494
<LinearLayout

libraries/sdk/src/main/res/layout/feed_post_item_single_image.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@
8080
android:layout_width="match_parent"
8181
android:layout_height="wrap_content"
8282
android:clickable="true"
83-
android:focusable="true">
83+
android:focusable="true"
84+
android:padding="0dp">
8485

8586
<ImageView
8687
android:id="@+id/mediaImageView"
87-
android:layout_width="match_parent"
88+
android:layout_width="match_parent"
8889
android:layout_height="wrap_content"
8990
android:clickable="true"
9091
android:focusable="true"
92+
android:adjustViewBounds="true"
93+
android:scaleType="fitCenter"
9194
android:foreground="?attr/selectableItemBackground"
9295
android:contentDescription="@string/post_image" />
9396

libraries/sdk/src/main/res/layout/feed_post_item_task.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090
android:layout_width="match_parent"
9191
android:layout_height="wrap_content"
9292
android:adjustViewBounds="true"
93-
android:maxHeight="300dp"
94-
android:scaleType="centerCrop"
93+
android:scaleType="fitCenter"
9594
android:clickable="true"
9695
android:focusable="true"
9796
android:foreground="?attr/selectableItemBackground"

0 commit comments

Comments
 (0)