24
24
import android .widget .ArrayAdapter ;
25
25
import android .widget .Button ;
26
26
import android .widget .EditText ;
27
+ import android .widget .FrameLayout ;
27
28
import android .widget .LinearLayout ;
28
29
import android .widget .ProgressBar ;
29
30
import android .widget .ScrollView ;
@@ -129,10 +130,10 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
129
130
130
131
private int initialListTop = 0 ;
131
132
133
+ @ BindView (R .id .mediaDetailFrameLayout )
134
+ FrameLayout frameLayout ;
132
135
@ BindView (R .id .mediaDetailImageView )
133
136
SimpleDraweeView image ;
134
- @ BindView (R .id .mediaDetailImageViewLandscape )
135
- SimpleDraweeView imageLandscape ;
136
137
@ BindView (R .id .mediaDetailImageViewSpacer )
137
138
LinearLayout imageSpacer ;
138
139
@ BindView (R .id .mediaDetailTitle )
@@ -211,6 +212,18 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
211
212
private Media media ;
212
213
private ArrayList <String > reasonList ;
213
214
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 ;
214
227
215
228
@ Override
216
229
public void onSaveInstanceState (Bundle outState ) {
@@ -274,7 +287,17 @@ && getParentFragment() instanceof MediaDetailPagerFragment) {
274
287
if (applicationKvStore .getBoolean ("login_skipped" )){
275
288
delete .setVisibility (GONE );
276
289
}
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
+ });
278
301
return view ;
279
302
}
280
303
@@ -315,9 +338,6 @@ public void onGlobalLayout() {
315
338
return ;
316
339
}
317
340
scrollView .getViewTreeObserver ().removeOnGlobalLayoutListener (this );
318
- if (getResources ().getConfiguration ().orientation == Configuration .ORIENTATION_LANDSCAPE ) {
319
- imageLandscape .setVisibility (VISIBLE );
320
- }
321
341
oldWidthOfImageView = scrollView .getWidth ();
322
342
displayMediaDetails ();
323
343
}
@@ -332,6 +352,16 @@ public void onConfigurationChanged(Configuration newConfig) {
332
352
new OnGlobalLayoutListener () {
333
353
@ Override
334
354
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
+ });
335
365
if (scrollView .getWidth () != oldWidthOfImageView ) {
336
366
if (newWidthOfImageView == 0 ) {
337
367
newWidthOfImageView = scrollView .getWidth ();
@@ -342,13 +372,7 @@ public void onGlobalLayout() {
342
372
}
343
373
}
344
374
);
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
352
376
if (heightVerifyingBoolean ) {
353
377
updateAspectRatio (newWidthOfImageView );
354
378
heightVerifyingBoolean = false ;
@@ -414,18 +438,32 @@ private void onDepictionsLoaded(List<IdAndCaptions> idAndCaptions){
414
438
* The imageSpacer is Basically a transparent overlay for the SimpleDraweeView
415
439
* which holds the image to be displayed( moreover this image is out of
416
440
* 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
+ *
417
447
* @param scrollWidth the current width of the scrollView
418
448
*/
419
449
private void updateAspectRatio (int scrollWidth ) {
420
450
if (imageInfoCache != null ) {
421
451
int finalHeight = (scrollWidth *imageInfoCache .getHeight ()) / imageInfoCache .getWidth ();
422
452
ViewGroup .LayoutParams params = image .getLayoutParams ();
423
453
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
+ }
424
463
params .height = finalHeight ;
425
464
spacerParams .height = finalHeight ;
426
465
image .setLayoutParams (params );
427
466
imageSpacer .setLayoutParams (spacerParams );
428
- imageLandscape .setLayoutParams (params );
429
467
}
430
468
}
431
469
@@ -452,23 +490,13 @@ private void setupImageView() {
452
490
image .getHierarchy ().setPlaceholderImage (R .drawable .image_placeholder );
453
491
image .getHierarchy ().setFailureImage (R .drawable .image_placeholder );
454
492
455
- imageLandscape .getHierarchy ().setPlaceholderImage (R .drawable .image_placeholder );
456
- imageLandscape .getHierarchy ().setFailureImage (R .drawable .image_placeholder );
457
-
458
493
DraweeController controller = Fresco .newDraweeControllerBuilder ()
459
494
.setLowResImageRequest (ImageRequest .fromUri (media .getThumbUrl ()))
460
495
.setImageRequest (ImageRequest .fromUri (media .getImageUrl ()))
461
496
.setControllerListener (aspectRatioListener )
462
497
.setOldController (image .getController ())
463
498
.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 ();
470
499
image .setController (controller );
471
- imageLandscape .setController (controllerLandscape );
472
500
}
473
501
474
502
/**
0 commit comments