diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderRelatedPostsView.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderRelatedPostsView.java index 8fea7d13c179..78f2981da2c2 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderRelatedPostsView.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderRelatedPostsView.java @@ -200,13 +200,22 @@ private void showFeaturedImage(final View postView, final ReaderRelatedPost rela } // featured image has height set to MATCH_PARENT so wait for parent view's layout to complete - // before loading image so we can set the image height correctly + // before loading image so we can set the image height correctly, then tell the imageView + // to crop the downloaded image to fit the exact width/height of the view postView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { postView.getViewTreeObserver().removeOnGlobalLayoutListener(this); - String photonUrl = PhotonUtils.getPhotonImageUrl(relatedPost.getFeaturedImageUrl(), mFeaturedImageWidth, postView.getHeight()); - imgFeatured.setImageUrl(photonUrl, WPNetworkImageView.ImageType.PHOTO); + int cropWidth = mFeaturedImageWidth; + int cropHeight = postView.getHeight(); + String photonUrl = PhotonUtils.getPhotonImageUrl( + relatedPost.getFeaturedImageUrl(), cropWidth, cropHeight); + imgFeatured.setImageUrl( + photonUrl, + WPNetworkImageView.ImageType.PHOTO, + null, + cropWidth, + cropHeight); } }); diff --git a/WordPress/src/main/java/org/wordpress/android/widgets/WPNetworkImageView.java b/WordPress/src/main/java/org/wordpress/android/widgets/WPNetworkImageView.java index 1f858664c534..05f7eaf61d65 100644 --- a/WordPress/src/main/java/org/wordpress/android/widgets/WPNetworkImageView.java +++ b/WordPress/src/main/java/org/wordpress/android/widgets/WPNetworkImageView.java @@ -6,6 +6,7 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; +import android.media.ThumbnailUtils; import android.os.AsyncTask; import android.support.annotation.ColorRes; import android.support.annotation.DrawableRes; @@ -60,6 +61,9 @@ public interface ImageLoadListener { private int mDefaultImageResId; private int mErrorImageResId; + private int mCropWidth; + private int mCropHeight; + private static final HashSet mUrlSkipList = new HashSet<>(); public WPNetworkImageView(Context context) { @@ -77,9 +81,25 @@ public void setImageUrl(String url, ImageType imageType) { } public void setImageUrl(String url, ImageType imageType, ImageLoadListener imageLoadListener) { + setImageUrl(url, imageType, imageLoadListener, 0, 0); + } + + public void setImageUrl(String url, + ImageType imageType, + ImageLoadListener imageLoadListener, + int cropWidth, + int cropHeight) { mUrl = url; mImageType = imageType; + if (cropWidth > 0 && cropHeight > 0) { + mCropWidth = cropWidth; + mCropHeight = cropHeight; + } else { + mCropWidth = 0; + mCropHeight = 0; + } + // The URL has potentially changed. See if we need to load it. loadImageIfNecessary(false, imageLoadListener); } @@ -258,6 +278,11 @@ private void handleResponse(ImageLoader.ImageContainer response, boolean isCache setVisibility(View.VISIBLE); } + // if cropping is requested, do it before further manipulation + if (mCropWidth > 0 && mCropHeight > 0) { + bitmap = ThumbnailUtils.extractThumbnail(bitmap, mCropWidth, mCropHeight); + } + // Apply circular rounding to avatars in a background task if (mImageType == ImageType.AVATAR) { new ShapeBitmapTask(ShapeType.CIRCLE, imageLoadListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, bitmap); diff --git a/WordPress/src/main/res/layout/reader_related_post.xml b/WordPress/src/main/res/layout/reader_related_post.xml index f7b10c662d68..6540b20835a7 100644 --- a/WordPress/src/main/res/layout/reader_related_post.xml +++ b/WordPress/src/main/res/layout/reader_related_post.xml @@ -17,7 +17,7 @@ android:layout_width="@dimen/reader_related_post_image_width" android:layout_height="match_parent" android:layout_marginRight="@dimen/margin_large" - android:scaleType="fitXY" + android:scaleType="centerCrop" android:visibility="gone" tools:src="@drawable/box_with_pages_top" tools:visibility="visible" />