From 9f560fefbc49c2157c3e93989c30c204a74a8798 Mon Sep 17 00:00:00 2001 From: Nader Jawad Date: Thu, 19 Sep 2019 15:59:53 -0700 Subject: [PATCH] Restored default behavior of GradientDrawable orientation Fixed issue where GradientDrawable orientation had diverged from the default behavior. Ensured that orientation by default is configured to LEFT_RIGHT for xml defined GradientDrawables and programmatically defined GradientDrawables have the same default orientation of TOP_BOTTOM. Refactored previous logic that would keep both the mAngle parameter used in xml inflation and the mOrientation parameter that is used to configure the angle of the gradient to no longer keep these 2 values in sync. Test: Added CTS test to verify orientation for xml and programmatically defined GradientDrawables Bug: 140835351 Bug: 139822941 Change-Id: I594496afe48d04d108053bf284e92bbfd3591fa3 --- .../graphics/drawable/GradientDrawable.java | 117 +++++------------- 1 file changed, 31 insertions(+), 86 deletions(-) diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index b9945cc735d8c..96ac0f9b38b55 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -637,7 +637,7 @@ public class GradientDrawable extends Drawable { * @see #setOrientation(Orientation) */ public Orientation getOrientation() { - return mGradientState.getOrientation(); + return mGradientState.mOrientation; } /** @@ -653,7 +653,7 @@ public class GradientDrawable extends Drawable { * @see #getOrientation() */ public void setOrientation(Orientation orientation) { - mGradientState.setOrientation(orientation); + mGradientState.mOrientation = orientation; mGradientIsDirty = true; invalidateSelf(); } @@ -1270,7 +1270,7 @@ public class GradientDrawable extends Drawable { if (st.mGradient == LINEAR_GRADIENT) { final float level = st.mUseLevel ? getLevel() / 10000.0f : 1.0f; - switch (st.getOrientation()) { + switch (st.mOrientation) { case TOP_BOTTOM: x0 = r.left; y0 = r.top; x1 = x0; y1 = level * r.bottom; @@ -1759,6 +1759,33 @@ public class GradientDrawable extends Drawable { int angle = (int) a.getFloat(R.styleable.GradientDrawableGradient_angle, st.mAngle); st.mAngle = ((angle % 360) + 360) % 360; // offset negative angle measures + switch (st.mAngle) { + case 0: + st.mOrientation = Orientation.LEFT_RIGHT; + break; + case 45: + st.mOrientation = Orientation.BL_TR; + break; + case 90: + st.mOrientation = Orientation.BOTTOM_TOP; + break; + case 135: + st.mOrientation = Orientation.BR_TL; + break; + case 180: + st.mOrientation = Orientation.RIGHT_LEFT; + break; + case 225: + st.mOrientation = Orientation.TR_BL; + break; + case 270: + st.mOrientation = Orientation.TOP_BOTTOM; + break; + case 315: + st.mOrientation = Orientation.TL_BR; + break; + } + final TypedValue tv = a.peekValue(R.styleable.GradientDrawableGradient_gradientRadius); if (tv != null) { final float radius; @@ -1981,7 +2008,7 @@ public class GradientDrawable extends Drawable { int[] mAttrPadding; public GradientState(Orientation orientation, int[] gradientColors) { - setOrientation(orientation); + mOrientation = orientation; setGradientColors(gradientColors); } @@ -2184,93 +2211,11 @@ 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;