Fix ChangeImageTransform for drawables without intrinsic size
Bug: 68489306 If we use a drawable like ColorDrawable for an ImageView it has intrinsicWidth and intrinsicHeight == -1. 1) Simplified matrix calculation in ChangeImageTransform.captureValues. It makes no sense to return null as a matrix because later in createAnimator it will be considered as the identity matrix. For cases when drawableSize == -1 instead we can just use view.getImageMatrix() which would be the identity matrix. 2) There is an additional check in createAnimator() to start an empty animation if the drawable width or height is 0. But actually it worth to use it also for cases when width or height is -1, as if it goes into else branch matrix transformations will try to incorrectly change the bounds of the drawable to (0, 0, -1, -1). 3) And also actually there is a bug in createNullAnimator() method, we can't provide nulls as values for ObjectAnimator.ofObject, as later it will crash on PropertyValuesHolder.setObjectValues(Object... values) because of "values[0].getClass()". So I changed it to provide some nonnull values like Matrix.IDENTITY_MATRIX. It is not important what to provide here as NULL_MATRIX_EVALUATOR will not use the values anyway. 4) Also I found a bug in ImageView.animateTransform(). If we provide null matrix if will try to update the drawable bounds to the view size but will not take into account paddings(in the same way like configureBounds()). Test: Tested manually on the sample app from the bug and added new tests for both cases: with view bounds change and without it Change-Id: I0750de56f4a011e06b340ed342884b59896d92dc
This commit is contained in:
@@ -97,22 +97,13 @@ public class ChangeImageTransform extends Transition {
|
||||
values.put(PROPNAME_BOUNDS, bounds);
|
||||
Matrix matrix;
|
||||
ImageView.ScaleType scaleType = imageView.getScaleType();
|
||||
if (scaleType == ImageView.ScaleType.FIT_XY) {
|
||||
matrix = imageView.getImageMatrix();
|
||||
if (!matrix.isIdentity()) {
|
||||
matrix = new Matrix(matrix);
|
||||
} else {
|
||||
int drawableWidth = drawable.getIntrinsicWidth();
|
||||
int drawableHeight = drawable.getIntrinsicHeight();
|
||||
if (drawableWidth > 0 && drawableHeight > 0) {
|
||||
float scaleX = ((float) bounds.width()) / drawableWidth;
|
||||
float scaleY = ((float) bounds.height()) / drawableHeight;
|
||||
matrix = new Matrix();
|
||||
matrix.setScale(scaleX, scaleY);
|
||||
} else {
|
||||
matrix = null;
|
||||
}
|
||||
}
|
||||
int drawableWidth = drawable.getIntrinsicWidth();
|
||||
int drawableHeight = drawable.getIntrinsicHeight();
|
||||
if (scaleType == ImageView.ScaleType.FIT_XY && drawableWidth > 0 && drawableHeight > 0) {
|
||||
float scaleX = ((float) bounds.width()) / drawableWidth;
|
||||
float scaleY = ((float) bounds.height()) / drawableHeight;
|
||||
matrix = new Matrix();
|
||||
matrix.setScale(scaleX, scaleY);
|
||||
} else {
|
||||
matrix = new Matrix(imageView.getImageMatrix());
|
||||
}
|
||||
@@ -152,17 +143,13 @@ public class ChangeImageTransform extends Transition {
|
||||
}
|
||||
Rect startBounds = (Rect) startValues.values.get(PROPNAME_BOUNDS);
|
||||
Rect endBounds = (Rect) endValues.values.get(PROPNAME_BOUNDS);
|
||||
if (startBounds == null || endBounds == null) {
|
||||
Matrix startMatrix = (Matrix) startValues.values.get(PROPNAME_MATRIX);
|
||||
Matrix endMatrix = (Matrix) endValues.values.get(PROPNAME_MATRIX);
|
||||
if (startBounds == null || endBounds == null || startMatrix == null || endMatrix == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Matrix startMatrix = (Matrix) startValues.values.get(PROPNAME_MATRIX);
|
||||
Matrix endMatrix = (Matrix) endValues.values.get(PROPNAME_MATRIX);
|
||||
|
||||
boolean matricesEqual = (startMatrix == null && endMatrix == null) ||
|
||||
(startMatrix != null && startMatrix.equals(endMatrix));
|
||||
|
||||
if (startBounds.equals(endBounds) && matricesEqual) {
|
||||
if (startBounds.equals(endBounds) && startMatrix.equals(endMatrix)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -172,15 +159,9 @@ public class ChangeImageTransform extends Transition {
|
||||
int drawableHeight = drawable.getIntrinsicHeight();
|
||||
|
||||
ObjectAnimator animator;
|
||||
if (drawableWidth == 0 || drawableHeight == 0) {
|
||||
if (drawableWidth <= 0 || drawableHeight <= 0) {
|
||||
animator = createNullAnimator(imageView);
|
||||
} else {
|
||||
if (startMatrix == null) {
|
||||
startMatrix = Matrix.IDENTITY_MATRIX;
|
||||
}
|
||||
if (endMatrix == null) {
|
||||
endMatrix = Matrix.IDENTITY_MATRIX;
|
||||
}
|
||||
ANIMATED_TRANSFORM_PROPERTY.set(imageView, startMatrix);
|
||||
animator = createMatrixAnimator(imageView, startMatrix, endMatrix);
|
||||
}
|
||||
@@ -189,7 +170,7 @@ public class ChangeImageTransform extends Transition {
|
||||
|
||||
private ObjectAnimator createNullAnimator(ImageView imageView) {
|
||||
return ObjectAnimator.ofObject(imageView, ANIMATED_TRANSFORM_PROPERTY,
|
||||
NULL_MATRIX_EVALUATOR, null, null);
|
||||
NULL_MATRIX_EVALUATOR, Matrix.IDENTITY_MATRIX, Matrix.IDENTITY_MATRIX);
|
||||
}
|
||||
|
||||
private ObjectAnimator createMatrixAnimator(final ImageView imageView, Matrix startMatrix,
|
||||
|
||||
@@ -1338,7 +1338,9 @@ public class ImageView extends View {
|
||||
return;
|
||||
}
|
||||
if (matrix == null) {
|
||||
mDrawable.setBounds(0, 0, getWidth(), getHeight());
|
||||
final int vwidth = getWidth() - mPaddingLeft - mPaddingRight;
|
||||
final int vheight = getHeight() - mPaddingTop - mPaddingBottom;
|
||||
mDrawable.setBounds(0, 0, vwidth, vheight);
|
||||
} else {
|
||||
mDrawable.setBounds(0, 0, mDrawableWidth, mDrawableHeight);
|
||||
if (mDrawMatrix == null) {
|
||||
|
||||
Reference in New Issue
Block a user