Commit 41acb940 authored by Tomasz Wiszkowski's avatar Tomasz Wiszkowski Committed by Commit Bot

Make RoundedCornerImageView respect paddings.

This is the smallest possible change that makes it possible to re-use
RCIV component with Suggestions.

Bug: 1001209
Change-Id: Idce813441f9757e4084e0a44ca706fda1c701ac8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1847898Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Ender <ender@google.com>
Cr-Commit-Position: refs/heads/master@{#703961}
parent ac88fe8c
...@@ -10,6 +10,7 @@ import android.graphics.Bitmap; ...@@ -10,6 +10,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapShader; import android.graphics.BitmapShader;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.Shader; import android.graphics.Shader;
...@@ -21,6 +22,7 @@ import android.graphics.drawable.shapes.Shape; ...@@ -21,6 +22,7 @@ import android.graphics.drawable.shapes.Shape;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.ColorRes; import androidx.annotation.ColorRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -44,6 +46,8 @@ public class RoundedCornerImageView extends ImageView { ...@@ -44,6 +46,8 @@ public class RoundedCornerImageView extends ImageView {
private Paint mPaint; private Paint mPaint;
private Paint mFillPaint; private Paint mFillPaint;
private final Matrix mScaleMatrix = new Matrix();
private boolean mRoundCorners;
// Object to avoid allocations during draw calls. // Object to avoid allocations during draw calls.
private final RectF mTmpRect = new RectF(); private final RectF mTmpRect = new RectF();
...@@ -90,6 +94,10 @@ public class RoundedCornerImageView extends ImageView { ...@@ -90,6 +94,10 @@ public class RoundedCornerImageView extends ImageView {
*/ */
public void setRoundedCorners(int cornerRadiusTopStart, int cornerRadiusTopEnd, public void setRoundedCorners(int cornerRadiusTopStart, int cornerRadiusTopEnd,
int cornerRadiusBottomStart, int cornerRadiusBottomEnd) { int cornerRadiusBottomStart, int cornerRadiusBottomEnd) {
mRoundCorners = (cornerRadiusTopStart != 0 || cornerRadiusTopEnd != 0
|| cornerRadiusBottomStart != 0 || cornerRadiusBottomEnd != 0);
if (!mRoundCorners) return;
float[] radii; float[] radii;
if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) { if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
radii = new float[] {cornerRadiusTopStart, cornerRadiusTopStart, cornerRadiusTopEnd, radii = new float[] {cornerRadiusTopStart, cornerRadiusTopStart, cornerRadiusTopEnd,
...@@ -108,7 +116,22 @@ public class RoundedCornerImageView extends ImageView { ...@@ -108,7 +116,22 @@ public class RoundedCornerImageView extends ImageView {
@Override @Override
public void setImageDrawable(@Nullable Drawable drawable) { public void setImageDrawable(@Nullable Drawable drawable) {
super.setImageDrawable(drawable); super.setImageDrawable(drawable);
reset();
}
@Override
public void setImageResource(int res) {
super.setImageResource(res);
reset();
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
reset();
}
private void reset() {
// Reset shaders. We will need to recalculate them. // Reset shaders. We will need to recalculate them.
mShader = null; mShader = null;
mApplyShader = false; mApplyShader = false;
...@@ -117,7 +140,6 @@ public class RoundedCornerImageView extends ImageView { ...@@ -117,7 +140,6 @@ public class RoundedCornerImageView extends ImageView {
if (mPaint != null) mPaint.setShader(null); if (mPaint != null) mPaint.setShader(null);
maybeCreateShader(); maybeCreateShader();
updateApplyShader(); updateApplyShader();
} }
...@@ -174,16 +196,22 @@ public class RoundedCornerImageView extends ImageView { ...@@ -174,16 +196,22 @@ public class RoundedCornerImageView extends ImageView {
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
final int width = getWidth() - getPaddingLeft() - getPaddingRight();
final int height = getHeight() - getPaddingTop() - getPaddingBottom();
Drawable drawable = getDrawable(); Drawable drawable = getDrawable();
Shape localRoundedRect = mRoundedRectangle; Shape localRoundedRect = mRoundedRectangle;
Paint localPaint = mPaint; Paint localPaint = mPaint;
boolean drawFill = mFillPaint != null && localRoundedRect != null boolean drawFill = mFillPaint != null && localRoundedRect != null
&& !(drawable instanceof ColorDrawable); && !(drawable instanceof ColorDrawable);
boolean drawContent = boolean drawContent = localPaint != null && localRoundedRect != null
localPaint != null && localRoundedRect != null && isSupportedDrawable(drawable); && isSupportedDrawable(drawable) && mRoundCorners;
if (drawFill || drawContent) localRoundedRect.resize(getWidth(), getHeight()); if (drawFill || drawContent) localRoundedRect.resize(width, height);
final int saveCount = canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
// First, fill the drawing area with the given fill paint. // First, fill the drawing area with the given fill paint.
if (drawFill) localRoundedRect.draw(canvas, mFillPaint); if (drawFill) localRoundedRect.draw(canvas, mFillPaint);
...@@ -191,6 +219,8 @@ public class RoundedCornerImageView extends ImageView { ...@@ -191,6 +219,8 @@ public class RoundedCornerImageView extends ImageView {
if (!drawContent) { if (!drawContent) {
// We probably have an unsupported drawable or we don't want rounded corners. Draw // We probably have an unsupported drawable or we don't want rounded corners. Draw
// normally and return. // normally and return.
// Undo our modifications to canvas first. ImageView will re-apply these.
canvas.restoreToCount(saveCount);
super.onDraw(canvas); super.onDraw(canvas);
return; return;
} }
...@@ -204,8 +234,11 @@ public class RoundedCornerImageView extends ImageView { ...@@ -204,8 +234,11 @@ public class RoundedCornerImageView extends ImageView {
if (mApplyShader) { if (mApplyShader) {
assert mShader != null; assert mShader != null;
// Apply the matrix to the bitmap shader. Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
mShader.setLocalMatrix(getImageMatrix()); mScaleMatrix.set(getImageMatrix());
mScaleMatrix.preScale(1.f * drawable.getIntrinsicWidth() / bitmap.getWidth(),
1.f * drawable.getIntrinsicHeight() / bitmap.getHeight());
mShader.setLocalMatrix(mScaleMatrix);
localPaint.setShader(mShader); localPaint.setShader(mShader);
// Find the desired bounding box where the bitmap is to be shown. // Find the desired bounding box where the bitmap is to be shown.
...@@ -213,8 +246,6 @@ public class RoundedCornerImageView extends ImageView { ...@@ -213,8 +246,6 @@ public class RoundedCornerImageView extends ImageView {
getImageMatrix().mapRect(mTmpRect); getImageMatrix().mapRect(mTmpRect);
} }
final int saveCount = canvas.save();
// Clip the canvas to the desired bounding box so that the shader isn't applied anywhere // Clip the canvas to the desired bounding box so that the shader isn't applied anywhere
// outside the desired area. // outside the desired area.
if (mApplyShader) canvas.clipRect(mTmpRect); if (mApplyShader) canvas.clipRect(mTmpRect);
......
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