Fixed issue where GradientDrawable orientation was not

consumed properly

Moved methods to resolve the current gradient orientation
from GradientDrawable to GradientState. Fixed issue where
orientation parameter was not consumed properly in the
GradientDrawable constructor causing the angle and orientation
parameters to be out of sync.

Bug: 132420435
Test: Added test to CTS to verify GradientDrawable constructor behavior
Change-Id: I639d1ab4b8791810ea72c3f85878a8c7d9093661
This commit is contained in:
Nader Jawad
2019-05-10 14:06:56 -07:00
parent 13a31b2648
commit b8a529ff1f

View File

@@ -637,8 +637,7 @@ public class GradientDrawable extends Drawable {
* @see #setOrientation(Orientation)
*/
public Orientation getOrientation() {
updateGradientStateOrientation();
return mGradientState.mOrientation;
return mGradientState.getOrientation();
}
/**
@@ -654,10 +653,7 @@ public class GradientDrawable extends Drawable {
* @see #getOrientation()
*/
public void setOrientation(Orientation orientation) {
// Update the angle here so that subsequent attempts to obtain the orientation
// from the angle overwrite previously configured values during inflation
mGradientState.mAngle = getAngleFromOrientation(orientation);
mGradientState.mOrientation = orientation;
mGradientState.setOrientation(orientation);
mGradientIsDirty = true;
invalidateSelf();
}
@@ -1245,76 +1241,6 @@ public class GradientDrawable extends Drawable {
return true;
}
/**
* Update the orientation of the gradient based on the given angle only if the type is
* {@link #LINEAR_GRADIENT}
*/
private void updateGradientStateOrientation() {
if (mGradientState.mGradient == LINEAR_GRADIENT) {
int angle = mGradientState.mAngle;
if (angle % 45 != 0) {
throw new IllegalArgumentException("Linear gradient requires 'angle' attribute to "
+ "be a multiple of 45");
}
Orientation orientation;
switch (angle) {
case 0:
orientation = Orientation.LEFT_RIGHT;
break;
case 45:
orientation = Orientation.BL_TR;
break;
case 90:
orientation = Orientation.BOTTOM_TOP;
break;
case 135:
orientation = Orientation.BR_TL;
break;
case 180:
orientation = Orientation.RIGHT_LEFT;
break;
case 225:
orientation = Orientation.TR_BL;
break;
case 270:
orientation = Orientation.TOP_BOTTOM;
break;
case 315:
orientation = Orientation.TL_BR;
break;
default:
// Should not get here as exception is thrown above if angle is not multiple
// of 45 degrees
orientation = Orientation.LEFT_RIGHT;
break;
}
mGradientState.mOrientation = orientation;
}
}
private int getAngleFromOrientation(Orientation orientation) {
switch (orientation) {
default:
case LEFT_RIGHT:
return 0;
case BL_TR:
return 45;
case BOTTOM_TOP:
return 90;
case BR_TL:
return 135;
case RIGHT_LEFT:
return 180;
case TR_BL:
return 225;
case TOP_BOTTOM:
return 270;
case TL_BR:
return 315;
}
}
/**
* This checks mGradientIsDirty, and if it is true, recomputes both our drawing
* rectangle (mRect) and the gradient itself, since it depends on our
@@ -1344,8 +1270,7 @@ public class GradientDrawable extends Drawable {
if (st.mGradient == LINEAR_GRADIENT) {
final float level = st.mUseLevel ? getLevel() / 10000.0f : 1.0f;
updateGradientStateOrientation();
switch (st.mOrientation) {
switch (st.getOrientation()) {
case TOP_BOTTOM:
x0 = r.left; y0 = r.top;
x1 = x0; y1 = level * r.bottom;
@@ -2056,7 +1981,7 @@ public class GradientDrawable extends Drawable {
int[] mAttrPadding;
public GradientState(Orientation orientation, int[] gradientColors) {
mOrientation = orientation;
setOrientation(orientation);
setGradientColors(gradientColors);
}
@@ -2259,6 +2184,93 @@ public class GradientDrawable extends Drawable {
mCenterY = y;
}
public void setOrientation(Orientation orientation) {
// Update the angle here so that subsequent attempts to obtain the orientation
// from the angle overwrite previously configured values during inflation
mAngle = getAngleFromOrientation(orientation);
mOrientation = orientation;
}
@NonNull
public Orientation getOrientation() {
updateGradientStateOrientation();
return mOrientation;
}
/**
* Update the orientation of the gradient based on the given angle only if the type is
* {@link #LINEAR_GRADIENT}
*/
private void updateGradientStateOrientation() {
if (mGradient == LINEAR_GRADIENT) {
int angle = mAngle;
if (angle % 45 != 0) {
throw new IllegalArgumentException("Linear gradient requires 'angle' attribute "
+ "to be a multiple of 45");
}
Orientation orientation;
switch (angle) {
case 0:
orientation = Orientation.LEFT_RIGHT;
break;
case 45:
orientation = Orientation.BL_TR;
break;
case 90:
orientation = Orientation.BOTTOM_TOP;
break;
case 135:
orientation = Orientation.BR_TL;
break;
case 180:
orientation = Orientation.RIGHT_LEFT;
break;
case 225:
orientation = Orientation.TR_BL;
break;
case 270:
orientation = Orientation.TOP_BOTTOM;
break;
case 315:
orientation = Orientation.TL_BR;
break;
default:
// Should not get here as exception is thrown above if angle is not multiple
// of 45 degrees
orientation = Orientation.LEFT_RIGHT;
break;
}
mOrientation = orientation;
}
}
private int getAngleFromOrientation(@Nullable Orientation orientation) {
if (orientation != null) {
switch (orientation) {
default:
case LEFT_RIGHT:
return 0;
case BL_TR:
return 45;
case BOTTOM_TOP:
return 90;
case BR_TL:
return 135;
case RIGHT_LEFT:
return 180;
case TR_BL:
return 225;
case TOP_BOTTOM:
return 270;
case TL_BR:
return 315;
}
} else {
return 0;
}
}
public void setGradientColors(@Nullable int[] colors) {
mGradientColors = colors;
mSolidColors = null;