summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorztenghui <ztenghui@google.com>2014-05-30 16:23:47 -0700
committerztenghui <ztenghui@google.com>2014-06-03 10:54:14 -0700
commit2af745b56cf4bdfdd9c05d90a4ccc740bf77a4fe (patch)
tree464b2de55681ccbd1e79520dc4c89ca2bb96213a
parent4d24caf1dec2babf273b18c99638fe2a0635ced4 (diff)
downloadframeworks_base-2af745b56cf4bdfdd9c05d90a4ccc740bf77a4fe.zip
frameworks_base-2af745b56cf4bdfdd9c05d90a4ccc740bf77a4fe.tar.gz
frameworks_base-2af745b56cf4bdfdd9c05d90a4ccc740bf77a4fe.tar.bz2
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
-rw-r--r--graphics/java/android/graphics/drawable/VectorDrawable.java107
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable06.xml4
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable12.xml1
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable14.xml1
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable17.xml1
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable18.xml1
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable19.xml1
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_drawable24.xml91
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_test01.xml3
-rw-r--r--tests/VectorDrawableTest/res/drawable/vector_test02.xml3
-rw-r--r--tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawable01.java2
-rw-r--r--tests/VectorDrawableTest/src/com/android/test/dynamic/VectorDrawablePerformance.java3
12 files changed, 168 insertions, 50 deletions
diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java
index 304502e..c6c5b31 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);
+ }
- final Paint strokePaint = mStrokePaint;
- if (vPath.mStrokeLineJoin != null) {
- strokePaint.setStrokeJoin(vPath.mStrokeLineJoin);
- }
+ if (vPath.mStrokeLineCap != null) {
+ strokePaint.setStrokeCap(vPath.mStrokeLineCap);
+ }
- if (vPath.mStrokeLineCap != null) {
- strokePaint.setStrokeCap(vPath.mStrokeLineCap);
- }
+ strokePaint.setStrokeMiter(vPath.mStrokeMiterlimit * scale);
- strokePaint.setStrokeMiter(vPath.mStrokeMiterlimit * scale);
- strokePaint.setColor(vPath.mStrokeColor);
- strokePaint.setStrokeWidth(vPath.mStrokeWidth * scale);
- canvas.drawPath(mRenderPath, strokePaint);
+ strokePaint.setColor(applyAlpha(vPath.mStrokeColor, stackedAlpha));
+ 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 850de28..ab5f7f4 100644
--- a/tests/VectorDrawableTest/res/drawable/vector_drawable06.xml
+++ b/tests/VectorDrawableTest/res/drawable/vector_drawable06.xml
@@ -25,10 +25,12 @@
<group>
<path android:pathData="M 569.374 461.472L 569.374 160.658L 160.658 160.658L 160.658 461.472L 569.374 461.472z"
android:name="path2451"
+ android:fill="#00000000"
android:stroke="#FF000000"
android:strokeWidth="30.65500000000000"/>
<path android:pathData="M 365.015 311.066"
android:name="path2453"
+ android:fill="#00000000"
android:stroke="#FF000000"
android:strokeWidth="30.655000000000001"/>
<path android:pathData="M 164.46 164.49L 340.78 343.158C 353.849 356.328 377.63 356.172 390.423 343.278L 566.622 165.928"
@@ -38,10 +40,12 @@
android:strokeWidth="30.655000000000001"/>
<path android:pathData="M 170.515 451.566L 305.61 313.46"
android:name="path2457"
+ android:fill="#00000000"
android:stroke="#000000"
android:strokeWidth="30.655000000000001"/>
<path android:pathData="M 557.968 449.974L 426.515 315.375"
android:name="path2459"
+ android:fill="#00000000"
android:stroke="#000000"
android:strokeWidth="30.655000000000001"/>
</group>
diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml
index d0edd8c..3042f6a 100644
--- a/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml
+++ b/tests/VectorDrawableTest/res/drawable/vector_drawable12.xml
@@ -27,6 +27,7 @@
<path
android:name="pie1"
android:pathData="M300,70 a230,230 0 1,0 1,0 z"
+ android:fill="#00000000"
android:stroke="#FF00FF00"
android:strokeWidth="70"
android:trimPathEnd=".75"
diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable14.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable14.xml
index 1abe1e1..8d4ca61 100644
--- a/tests/VectorDrawableTest/res/drawable/vector_drawable14.xml
+++ b/tests/VectorDrawableTest/res/drawable/vector_drawable14.xml
@@ -34,6 +34,7 @@
a25,25 -30 0,1 100,-50 l 50,-25
a25,37 -30 0,1 100,-50 l 50,-25
a25,50 -30 0,1 100,-50 l 50,-25"
+ android:fill="#00000000"
android:stroke="#FF00FF00"
android:strokeWidth="10" />
</group>
diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml
index 8e98d02..c28aff4 100644
--- a/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml
+++ b/tests/VectorDrawableTest/res/drawable/vector_drawable17.xml
@@ -25,6 +25,7 @@
<path
android:name="house"
android:pathData="M200,300 Q400,50 600,300 T1000,300"
+ android:fill="#00000000"
android:stroke="#FFFF0000"
android:strokeWidth="10"/>
</group>
diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml
index 6d74ebd..d7042fd 100644
--- a/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml
+++ b/tests/VectorDrawableTest/res/drawable/vector_drawable18.xml
@@ -27,6 +27,7 @@
<path
android:name="house"
android:pathData="M100,200 C100,100 250,100 250,200 S400,300 400,200"
+ android:fill="#00000000"
android:stroke="#FFFFFF00"
android:strokeWidth="10" />
</group>
diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable19.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable19.xml
index a890fd6..47a9574 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" />
</group>
diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable24.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable24.xml
new file mode 100644
index 0000000..c062d70
--- /dev/null
+++ b/tests/VectorDrawableTest/res/drawable/vector_drawable24.xml
@@ -0,0 +1,91 @@
+<!--
+ Copyright (C) 2014 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android" >
+
+ <size
+ android:height="64dp"
+ android:width="64dp" />
+
+ <viewport
+ android:viewportHeight="400"
+ android:viewportWidth="400" />
+
+ <group android:name="backgroundGroup"
+ android:alpha = "0.5" >
+ <path
+ android:name="background1"
+ android:fill="#FF000000"
+ android:pathData="M 0,0 l 200,0 l 0, 200 l -200, 0 z" />
+ <path
+ android:name="background2"
+ android:fill="#FF000000"
+ android:pathData="M 200,200 l 200,0 l 0, 200 l -200, 0 z" />
+ </group>
+ <group
+ android:name="translateToCenterGroup"
+ android:translateX="50.0"
+ android:translateY="90.0"
+ android:alpha = "0.5" >
+ <path
+ android:name="twoLines"
+ android:pathData="@string/twoLinePathData"
+ android:stroke="#FFFF0000"
+ android:strokeWidth="20" />
+
+ <group
+ android:name="rotationGroup"
+ android:pivotX="0.0"
+ android:pivotY="0.0"
+ android:rotation="-45.0"
+ android:alpha = "0.5" >
+ <path
+ android:name="twoLines1"
+ android:pathData="@string/twoLinePathData"
+ android:stroke="#FF00FF00"
+ android:strokeWidth="20" />
+
+ <group
+ android:name="translateGroup"
+ android:translateX="130.0"
+ android:translateY="160.0"
+ android:alpha = "0.5">
+ <group android:name="scaleGroup" >
+ <path
+ android:name="twoLines3"
+ android:pathData="@string/twoLinePathData"
+ android:stroke="#FF0000FF"
+ android:strokeWidth="20" />
+ </group>
+ </group>
+
+ <group
+ android:name="translateGroupHalf"
+ android:translateX="65.0"
+ android:translateY="80.0"
+ android:alpha = "0.5">
+ <group android:name="scaleGroup" >
+ <path
+ android:name="twoLines2"
+ android:pathData="@string/twoLinePathData"
+ android:fill="?android:attr/colorForeground"
+ android:stroke="?android:attr/colorForeground"
+ android:strokeWidth="20" />
+ </group>
+ </group>
+ </group>
+ </group>
+
+</vector> \ 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 a9091ab..fc2a15c 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" />
</group>
</vector> \ 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 ab58c06..9f4abbf 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" />
</group>
</vector> \ 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 7ba01b1..a23d819 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 c2a5e6b..814deb8 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