Skip to content

Commit 9cf59f7

Browse files
authored
fixes commons-app#4266 'Image Dimensions should be reduced if too large" (commons-app#4279)
* height restriction * requested changes * removed landscape drawee view * javadoc
1 parent 9fd6521 commit 9cf59f7

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.widget.ArrayAdapter;
2525
import android.widget.Button;
2626
import android.widget.EditText;
27+
import android.widget.FrameLayout;
2728
import android.widget.LinearLayout;
2829
import android.widget.ProgressBar;
2930
import android.widget.ScrollView;
@@ -129,10 +130,10 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
129130

130131
private int initialListTop = 0;
131132

133+
@BindView(R.id.mediaDetailFrameLayout)
134+
FrameLayout frameLayout;
132135
@BindView(R.id.mediaDetailImageView)
133136
SimpleDraweeView image;
134-
@BindView(R.id.mediaDetailImageViewLandscape)
135-
SimpleDraweeView imageLandscape;
136137
@BindView(R.id.mediaDetailImageViewSpacer)
137138
LinearLayout imageSpacer;
138139
@BindView(R.id.mediaDetailTitle)
@@ -211,6 +212,18 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
211212
private Media media;
212213
private ArrayList<String> reasonList;
213214

215+
/**
216+
* Height stores the height of the frame layout as soon as it is initialised and updates itself on
217+
* configuration changes.
218+
* Used to adjust aspect ratio of image when length of the image is too large.
219+
*/
220+
private int frameLayoutHeight;
221+
222+
/**
223+
* Minimum height of the metadata, in pixels.
224+
* Images with a very narrow aspect ratio will be reduced so that the metadata information panel always has at least this height.
225+
*/
226+
private int minimumHeightOfMetadata = 200;
214227

215228
@Override
216229
public void onSaveInstanceState(Bundle outState) {
@@ -274,7 +287,17 @@ && getParentFragment() instanceof MediaDetailPagerFragment) {
274287
if(applicationKvStore.getBoolean("login_skipped")){
275288
delete.setVisibility(GONE);
276289
}
277-
290+
/**
291+
* Gets the height of the frame layout as soon as the view is ready and updates aspect ratio
292+
* of the picture.
293+
*/
294+
view.post(new Runnable() {
295+
@Override
296+
public void run() {
297+
frameLayoutHeight = frameLayout.getMeasuredHeight();
298+
updateAspectRatio(scrollView.getWidth());
299+
}
300+
});
278301
return view;
279302
}
280303

@@ -315,9 +338,6 @@ public void onGlobalLayout() {
315338
return;
316339
}
317340
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
318-
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
319-
imageLandscape.setVisibility(VISIBLE);
320-
}
321341
oldWidthOfImageView = scrollView.getWidth();
322342
displayMediaDetails();
323343
}
@@ -332,6 +352,16 @@ public void onConfigurationChanged(Configuration newConfig) {
332352
new OnGlobalLayoutListener() {
333353
@Override
334354
public void onGlobalLayout() {
355+
/**
356+
* We update the height of the frame layout as the configuration changes.
357+
*/
358+
frameLayout.post(new Runnable() {
359+
@Override
360+
public void run() {
361+
frameLayoutHeight = frameLayout.getMeasuredHeight();
362+
updateAspectRatio(scrollView.getWidth());
363+
}
364+
});
335365
if (scrollView.getWidth() != oldWidthOfImageView) {
336366
if (newWidthOfImageView == 0) {
337367
newWidthOfImageView = scrollView.getWidth();
@@ -342,13 +372,7 @@ public void onGlobalLayout() {
342372
}
343373
}
344374
);
345-
// check orientation
346-
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
347-
imageLandscape.setVisibility(VISIBLE);
348-
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
349-
imageLandscape.setVisibility(GONE);
350-
}
351-
// ensuring correct aspect ratio for landscape mode
375+
// Ensuring correct aspect ratio for landscape mode
352376
if (heightVerifyingBoolean) {
353377
updateAspectRatio(newWidthOfImageView);
354378
heightVerifyingBoolean = false;
@@ -414,18 +438,32 @@ private void onDepictionsLoaded(List<IdAndCaptions> idAndCaptions){
414438
* The imageSpacer is Basically a transparent overlay for the SimpleDraweeView
415439
* which holds the image to be displayed( moreover this image is out of
416440
* the scroll view )
441+
*
442+
*
443+
* If the image is sufficiently large i.e. the image height extends the view height, we reduce
444+
* the height and change the width to maintain the aspect ratio, otherwise image takes up the
445+
* total possible width and height is adjusted accordingly.
446+
*
417447
* @param scrollWidth the current width of the scrollView
418448
*/
419449
private void updateAspectRatio(int scrollWidth) {
420450
if (imageInfoCache != null) {
421451
int finalHeight = (scrollWidth*imageInfoCache.getHeight()) / imageInfoCache.getWidth();
422452
ViewGroup.LayoutParams params = image.getLayoutParams();
423453
ViewGroup.LayoutParams spacerParams = imageSpacer.getLayoutParams();
454+
params.width = scrollWidth;
455+
if(finalHeight > frameLayoutHeight - minimumHeightOfMetadata) {
456+
457+
// Adjust the height and width of image.
458+
int temp = frameLayoutHeight - minimumHeightOfMetadata;
459+
params.width = (scrollWidth*temp) / finalHeight;
460+
finalHeight = temp;
461+
462+
}
424463
params.height = finalHeight;
425464
spacerParams.height = finalHeight;
426465
image.setLayoutParams(params);
427466
imageSpacer.setLayoutParams(spacerParams);
428-
imageLandscape.setLayoutParams(params);
429467
}
430468
}
431469

@@ -452,23 +490,13 @@ private void setupImageView() {
452490
image.getHierarchy().setPlaceholderImage(R.drawable.image_placeholder);
453491
image.getHierarchy().setFailureImage(R.drawable.image_placeholder);
454492

455-
imageLandscape.getHierarchy().setPlaceholderImage(R.drawable.image_placeholder);
456-
imageLandscape.getHierarchy().setFailureImage(R.drawable.image_placeholder);
457-
458493
DraweeController controller = Fresco.newDraweeControllerBuilder()
459494
.setLowResImageRequest(ImageRequest.fromUri(media.getThumbUrl()))
460495
.setImageRequest(ImageRequest.fromUri(media.getImageUrl()))
461496
.setControllerListener(aspectRatioListener)
462497
.setOldController(image.getController())
463498
.build();
464-
DraweeController controllerLandscape = Fresco.newDraweeControllerBuilder()
465-
.setLowResImageRequest(ImageRequest.fromUri(media.getThumbUrl()))
466-
.setImageRequest(ImageRequest.fromUri(media.getImageUrl()))
467-
.setControllerListener(aspectRatioListener)
468-
.setOldController(imageLandscape.getController())
469-
.build();
470499
image.setController(controller);
471-
imageLandscape.setController(controllerLandscape);
472500
}
473501

474502
/**

app/src/main/res/layout/fragment_media_detail.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
88
android:background="?attr/mainBackground"
9+
android:id="@+id/mediaDetailFrameLayout"
910
>
1011

1112
<LinearLayout
@@ -34,8 +35,9 @@
3435

3536
<com.facebook.drawee.view.SimpleDraweeView
3637
android:id="@+id/mediaDetailImageView"
37-
android:layout_width="match_parent"
38+
android:layout_width="wrap_content"
3839
android:layout_height="@dimen/dimen_250"
40+
android:layout_gravity="center_horizontal"
3941
app:actualImageScaleType="none" />
4042

4143
<ScrollView
@@ -58,13 +60,6 @@
5860
android:layout_width="match_parent"
5961
android:layout_height="wrap_content" >
6062

61-
<com.facebook.drawee.view.SimpleDraweeView
62-
android:id="@+id/mediaDetailImageViewLandscape"
63-
android:layout_width="match_parent"
64-
android:layout_height="@dimen/dimen_250"
65-
app:actualImageScaleType="none"
66-
android:visibility="gone" />
67-
6863
<LinearLayout
6964
android:layout_width="match_parent"
7065
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)