Commit d03d3e45 authored by David Trainor's avatar David Trainor Committed by Commit Bot

Fix RoundedCornerImageView issues

RoundedCornerImageView ran into a few problems exposed by omnibox usage:
- ImageView#setImageResource() doesn't end up calling setImageDrawable()
on some Android versions.
- BitmapDrawable can apply a scale so getIntrinsicWidth/Height won't be
the same as get Bitmap#getWidth/Height.  This wasn't handled in this
code.
- Also some general code clean-up to make it easier to parse and update
in the future.

Bug: 1001209
Change-Id: Ib861686ce51eb019cc068e24e042aca1ae42cd23
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1788299
Commit-Queue: Ender <ender@google.com>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Auto-Submit: David Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706645}
parent 9d83031b
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package org.chromium.chrome.browser.download.home.list.holder; package org.chromium.chrome.browser.download.home.list.holder;
import android.graphics.Bitmap;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
...@@ -193,25 +192,33 @@ class OfflineItemViewHolder extends ListItemViewHolder implements ListMenuButton ...@@ -193,25 +192,33 @@ class OfflineItemViewHolder extends ListItemViewHolder implements ListMenuButton
@Override @Override
public void maybeResizeImage(Drawable drawable) { public void maybeResizeImage(Drawable drawable) {
if (!(drawable instanceof BitmapDrawable)) return; Matrix matrix = null;
if (drawable instanceof BitmapDrawable) {
matrix = upscaleBitmapIfNecessary((BitmapDrawable) drawable);
}
Matrix matrix = upscaleBitmapIfNecessary(((BitmapDrawable) drawable).getBitmap());
mImageView.setImageMatrix(matrix); mImageView.setImageMatrix(matrix);
mImageView.setScaleType( mImageView.setScaleType(
matrix == null ? ImageView.ScaleType.CENTER_CROP : ImageView.ScaleType.MATRIX); matrix == null ? ImageView.ScaleType.CENTER_CROP : ImageView.ScaleType.MATRIX);
} }
private Matrix upscaleBitmapIfNecessary(Bitmap bitmap) { private Matrix upscaleBitmapIfNecessary(BitmapDrawable drawable) {
if (bitmap == null) return null; if (drawable == null) return null;
float scale = computeScaleFactor(bitmap); int width = drawable.getBitmap().getWidth();
int height = drawable.getBitmap().getHeight();
float scale = computeScaleFactor(width, height);
if (scale <= 1.f) return null; if (scale <= 1.f) return null;
// Compute the required matrix to scale and center the bitmap. // Compute the required matrix to scale and center the bitmap.
float dx = (mImageView.getWidth() - width * scale) / 2.f;
float dy = (mImageView.getHeight() - height * scale) / 2.f;
Matrix matrix = new Matrix(); Matrix matrix = new Matrix();
matrix.setScale(scale, scale); matrix.postScale(scale, scale);
matrix.postTranslate((mImageView.getWidth() - scale * bitmap.getWidth()) * 0.5f, matrix.postTranslate(dx, dy);
(mImageView.getHeight() - scale * bitmap.getHeight()) * 0.5f);
return matrix; return matrix;
} }
...@@ -220,9 +227,9 @@ class OfflineItemViewHolder extends ListItemViewHolder implements ListMenuButton ...@@ -220,9 +227,9 @@ class OfflineItemViewHolder extends ListItemViewHolder implements ListMenuButton
* dimensions. The scaled bitmap will be centered inside the view. No scaling if the * dimensions. The scaled bitmap will be centered inside the view. No scaling if the
* dimensions are comparable. * dimensions are comparable.
*/ */
private float computeScaleFactor(Bitmap bitmap) { private float computeScaleFactor(int width, int height) {
float widthRatio = (float) mImageView.getWidth() / bitmap.getWidth(); float widthRatio = (float) mImageView.getWidth() / width;
float heightRatio = (float) mImageView.getHeight() / bitmap.getHeight(); float heightRatio = (float) mImageView.getHeight() / height;
UmaUtils.recordImageViewRequiredStretch(widthRatio, heightRatio, mFilter); UmaUtils.recordImageViewRequiredStretch(widthRatio, heightRatio, mFilter);
if (Math.max(widthRatio, heightRatio) < IMAGE_VIEW_MAX_SCALE_FACTOR) return 1.f; if (Math.max(widthRatio, heightRatio) < IMAGE_VIEW_MAX_SCALE_FACTOR) return 1.f;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment