From 2af745b56cf4bdfdd9c05d90a4ccc740bf77a4fe Mon Sep 17 00:00:00 2001 From: ztenghui Date: Fri, 30 May 2014 16:23:47 -0700 Subject: [PATCH] Handle the alpha in the group setting and fix the fill issue. The fill used to be dafault as "none". Now it is default to opaque black, which is consistent with svg now. So it is better to always set the fill value. bug:15288554 Change-Id: I07ac41c5adc77665e66e52f2c0220139586fb09b --- .../graphics/drawable/VectorDrawable.java | 111 ++++++++++-------- .../res/drawable/vector_drawable06.xml | 4 + .../res/drawable/vector_drawable12.xml | 1 + .../res/drawable/vector_drawable14.xml | 1 + .../res/drawable/vector_drawable17.xml | 1 + .../res/drawable/vector_drawable18.xml | 1 + .../res/drawable/vector_drawable19.xml | 1 + .../res/drawable/vector_drawable24.xml | 91 ++++++++++++++ .../res/drawable/vector_test01.xml | 3 +- .../res/drawable/vector_test02.xml | 3 +- .../test/dynamic/VectorDrawable01.java | 2 + .../dynamic/VectorDrawablePerformance.java | 3 +- 12 files changed, 170 insertions(+), 52 deletions(-) create mode 100644 tests/VectorDrawableTest/res/drawable/vector_drawable24.xml diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java index 304502ee7651a..c6c5b31bfe180 100644 --- a/graphics/java/android/graphics/drawable/VectorDrawable.java +++ b/graphics/java/android/graphics/drawable/VectorDrawable.java @@ -18,6 +18,7 @@ import android.content.res.Resources; import android.content.res.Resources.Theme; import android.content.res.TypedArray; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Matrix; import android.graphics.Paint; @@ -134,8 +135,6 @@ public class VectorDrawable extends Drawable { private final VectorDrawableState mVectorState; - private int mAlpha = 0xFF; - public VectorDrawable() { mVectorState = new VectorDrawableState(null); } @@ -164,9 +163,8 @@ public class VectorDrawable extends Drawable { @Override public void setAlpha(int alpha) { - // TODO correct handling of transparent - if (mAlpha != alpha) { - mAlpha = alpha; + if (mVectorState.mVPathRenderer.getRootAlpha() != alpha) { + mVectorState.mVPathRenderer.setRootAlpha(alpha); invalidateSelf(); } } @@ -273,6 +271,13 @@ public class VectorDrawable extends Drawable { return null; } + private static int applyAlpha(int color, float alpha) { + int alphaBytes = Color.alpha(color); + color &= 0x00FFFFFF; + color |= ((int) (alphaBytes * alpha)) << 24; + return color; + } + private VPathRenderer inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme) throws XmlPullParserException, IOException { final VPathRenderer pathRenderer = new VPathRenderer(); @@ -434,6 +439,7 @@ public class VectorDrawable extends Drawable { private float mBaseHeight = 0; private float mViewportWidth = 0; private float mViewportHeight = 0; + private int mRootAlpha = 0xFF; private final Matrix mFinalPathMatrix = new Matrix(); @@ -441,6 +447,14 @@ public class VectorDrawable extends Drawable { mRootGroup = new VGroup(); } + public void setRootAlpha(int alpha) { + mRootAlpha = alpha; + } + + public int getRootAlpha() { + return mRootAlpha; + } + public VPathRenderer(VPathRenderer copy) { mRootGroup = copy.mRootGroup; mBaseWidth = copy.mBaseWidth; @@ -519,7 +533,7 @@ public class VectorDrawable extends Drawable { } private void drawGroupTree(VGroup currentGroup, Matrix currentMatrix, - Canvas canvas, int w, int h) { + float currentAlpha, Canvas canvas, int w, int h) { // Calculate current group's matrix by preConcat the parent's and // and the current one on the top of the stack. // Basically the Mfinal = Mviewport * M0 * M1 * M2; @@ -528,20 +542,21 @@ public class VectorDrawable extends Drawable { currentGroup.mStackedMatrix.preConcat(currentGroup.mLocalMatrix); - drawPath(currentGroup, canvas, w, h); + float stackedAlpha = currentAlpha * currentGroup.mGroupAlpha; + drawPath(currentGroup, stackedAlpha, canvas, w, h); // Draw the group tree in post order. for (int i = 0 ; i < currentGroup.mChildGroupList.size(); i++) { drawGroupTree(currentGroup.mChildGroupList.get(i), - currentGroup.mStackedMatrix, canvas, w, h); + currentGroup.mStackedMatrix, stackedAlpha, canvas, w, h); } } public void draw(Canvas canvas, int w, int h) { // Travese the tree in pre-order to draw. - drawGroupTree(mRootGroup, IDENTITY_MATRIX, canvas, w, h); + drawGroupTree(mRootGroup, IDENTITY_MATRIX, ((float) mRootAlpha) / 0xFF, canvas, w, h); } - private void drawPath(VGroup vGroup, Canvas canvas, int w, int h) { + private void drawPath(VGroup vGroup, float stackedAlpha, Canvas canvas, int w, int h) { final float scale = Math.min(h / mViewportHeight, w / mViewportWidth); mFinalPathMatrix.set(vGroup.mStackedMatrix); @@ -582,41 +597,41 @@ public class VectorDrawable extends Drawable { if (vPath.mClip) { canvas.clipPath(mRenderPath, Region.Op.REPLACE); - } - - if (vPath.mFillColor != 0) { - if (mFillPaint == null) { - mFillPaint = new Paint(); - mFillPaint.setColorFilter(mColorFilter); - mFillPaint.setStyle(Paint.Style.FILL); - mFillPaint.setAntiAlias(true); + } else { + if (vPath.mFillColor != 0) { + if (mFillPaint == null) { + mFillPaint = new Paint(); + mFillPaint.setColorFilter(mColorFilter); + mFillPaint.setStyle(Paint.Style.FILL); + mFillPaint.setAntiAlias(true); + } + mFillPaint.setColor(applyAlpha(vPath.mFillColor, stackedAlpha)); + canvas.drawPath(mRenderPath, mFillPaint); } - mFillPaint.setColor(vPath.mFillColor); - canvas.drawPath(mRenderPath, mFillPaint); - } + if (vPath.mStrokeColor != 0) { + if (mStrokePaint == null) { + mStrokePaint = new Paint(); + mStrokePaint.setColorFilter(mColorFilter); + mStrokePaint.setStyle(Paint.Style.STROKE); + mStrokePaint.setAntiAlias(true); + } - if (vPath.mStrokeColor != 0) { - if (mStrokePaint == null) { - mStrokePaint = new Paint(); - mStrokePaint.setColorFilter(mColorFilter); - mStrokePaint.setStyle(Paint.Style.STROKE); - mStrokePaint.setAntiAlias(true); + final Paint strokePaint = mStrokePaint; + if (vPath.mStrokeLineJoin != null) { + strokePaint.setStrokeJoin(vPath.mStrokeLineJoin); + } + + if (vPath.mStrokeLineCap != null) { + strokePaint.setStrokeCap(vPath.mStrokeLineCap); + } + + strokePaint.setStrokeMiter(vPath.mStrokeMiterlimit * scale); + + strokePaint.setColor(applyAlpha(vPath.mStrokeColor, stackedAlpha)); + strokePaint.setStrokeWidth(vPath.mStrokeWidth * scale); + canvas.drawPath(mRenderPath, strokePaint); } - - final Paint strokePaint = mStrokePaint; - if (vPath.mStrokeLineJoin != null) { - strokePaint.setStrokeJoin(vPath.mStrokeLineJoin); - } - - if (vPath.mStrokeLineCap != null) { - strokePaint.setStrokeCap(vPath.mStrokeLineCap); - } - - strokePaint.setStrokeMiter(vPath.mStrokeMiterlimit * scale); - strokePaint.setColor(vPath.mStrokeColor); - strokePaint.setStrokeWidth(vPath.mStrokeWidth * scale); - canvas.drawPath(mRenderPath, strokePaint); } } } @@ -669,7 +684,7 @@ public class VectorDrawable extends Drawable { private float mScaleY = 1; private float mTranslateX = 0; private float mTranslateY = 0; - private float mAlpha = 1; + private float mGroupAlpha = 1; // mLocalMatrix is parsed from the XML. private final Matrix mLocalMatrix = new Matrix(); @@ -714,7 +729,7 @@ public class VectorDrawable extends Drawable { mScaleY = a.getFloat(R.styleable.VectorDrawableGroup_scaleY, mScaleY); mTranslateX = a.getFloat(R.styleable.VectorDrawableGroup_translateX, mTranslateX); mTranslateY = a.getFloat(R.styleable.VectorDrawableGroup_translateY, mTranslateY); - mAlpha = a.getFloat(R.styleable.VectorDrawableGroup_alpha, mAlpha); + mGroupAlpha = a.getFloat(R.styleable.VectorDrawableGroup_alpha, mGroupAlpha); updateLocalMatrix(); if (a.hasValue(R.styleable.VectorDrawableGroup_name)) { mName = a.getString(R.styleable.VectorDrawableGroup_name); @@ -763,7 +778,7 @@ public class VectorDrawable extends Drawable { } if (themeAttrs == null || themeAttrs[R.styleable.VectorDrawableGroup_alpha] == 0) { - mAlpha = a.getFloat(R.styleable.VectorDrawableGroup_alpha, mAlpha); + mGroupAlpha = a.getFloat(R.styleable.VectorDrawableGroup_alpha, mGroupAlpha); } updateLocalMatrix(); @@ -799,7 +814,7 @@ public class VectorDrawable extends Drawable { float mStrokeWidth = 0; float mStrokeOpacity = Float.NaN; - int mFillColor = 0; + int mFillColor = Color.BLACK; int mFillRule; float mFillOpacity = Float.NaN; @@ -984,13 +999,11 @@ public class VectorDrawable extends Drawable { private void updateColorAlphas() { if (!Float.isNaN(mFillOpacity)) { - mFillColor &= 0x00FFFFFF; - mFillColor |= ((int) (0xFF * mFillOpacity)) << 24; + mFillColor = applyAlpha(mFillColor, mFillOpacity); } if (!Float.isNaN(mStrokeOpacity)) { - mStrokeColor &= 0x00FFFFFF; - mStrokeColor |= ((int) (0xFF * mStrokeOpacity)) << 24; + mStrokeColor = applyAlpha(mStrokeColor, mStrokeOpacity); } } diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable06.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable06.xml index 850de28965000..ab5f7f49ba3a7 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable06.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable06.xml @@ -25,10 +25,12 @@ diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml index d0edd8cf199ff..3042f6ab3017f 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml @@ -27,6 +27,7 @@ diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml index 8e98d02513d38..c28aff46cd89e 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml @@ -25,6 +25,7 @@ diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml index 6d74ebd96ce99..d7042fd0b2baa 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml @@ -27,6 +27,7 @@ diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable19.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable19.xml index a890fd6a8874e..47a9574eb681f 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable19.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable19.xml @@ -29,6 +29,7 @@ android:pathData="M10,300 Q400,550 600,300 T1000,300" android:pivotX="90" android:pivotY="100" + android:fill="#00000000" android:stroke="#FFFF0000" android:strokeWidth="60" /> diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable24.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable24.xml new file mode 100644 index 0000000000000..c062d702f0d8d --- /dev/null +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable24.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/VectorDrawableTest/res/drawable/vector_test01.xml b/tests/VectorDrawableTest/res/drawable/vector_test01.xml index a9091aba38277..fc2a15c11b239 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_test01.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_test01.xml @@ -28,7 +28,8 @@ limitations under the License. android:name="002b" android:pathData="M100,200c0,-100 150,-100 150,0s150,100 150,0t-200,299" android:stroke="#FF0000FF" - android:strokeWidth="4" /> + android:strokeWidth="4" + android:fill="#00000000" /> \ No newline at end of file diff --git a/tests/VectorDrawableTest/res/drawable/vector_test02.xml b/tests/VectorDrawableTest/res/drawable/vector_test02.xml index ab58c0674eab8..9f4abbff5d919 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_test02.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_test02.xml @@ -28,7 +28,8 @@ limitations under the License. android:name="002b" android:pathData="M100,200c0,-100 150,-100 150,0s150,100 150,0T-200,299" android:stroke="#FF0000FF" - android:strokeWidth="4" /> + android:strokeWidth="4" + android:fill="#00000000" /> \ No newline at end of file diff --git a/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawable01.java b/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawable01.java index 7ba01b1359e06..a23d819337495 100644 --- a/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawable01.java +++ b/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawable01.java @@ -61,6 +61,8 @@ public class VectorDrawable01 extends Activity { button.setWidth(200); button.setBackgroundResource(icon[i]); container.addView(button); + VectorDrawable vd = (VectorDrawable) button.getBackground(); + vd.setAlpha((i + 1) * (0xFF / (icon.length + 1))); } setContentView(container); diff --git a/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawablePerformance.java b/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawablePerformance.java index c2a5e6b3134c3..814deb8894d59 100644 --- a/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawablePerformance.java +++ b/tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawablePerformance.java @@ -50,7 +50,8 @@ public class VectorDrawablePerformance extends Activity { R.drawable.vector_drawable20, R.drawable.vector_drawable21, R.drawable.vector_drawable22, - R.drawable.vector_drawable23 + R.drawable.vector_drawable23, + R.drawable.vector_drawable24, }; @Override