From fd6e411767735478ee0f69ba01d77def4c6b2627 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Fri, 23 May 2014 10:10:33 -0700 Subject: Add support for setColorFilter in VectorDrawable Also cleans up inflation so that we can apply themes more easily. Change-Id: I56decb29fe987286a38fc0aa96f95952cf67c120 --- .../android/graphics/drawable/VectorDrawable.java | 56 ++++++++++++++++------ 1 file changed, 41 insertions(+), 15 deletions(-) (limited to 'graphics') diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java index e3ed75e..e2bd50d 100644 --- a/graphics/java/android/graphics/drawable/VectorDrawable.java +++ b/graphics/java/android/graphics/drawable/VectorDrawable.java @@ -159,7 +159,8 @@ public class VectorDrawable extends Drawable { @Override public void setColorFilter(ColorFilter colorFilter) { - // TODO: support color filter + mVectorState.mVPathRenderer.setColorFilter(colorFilter); + invalidateSelf(); } @Override @@ -365,14 +366,15 @@ public class VectorDrawable extends Drawable { private VPath[] mCurrentPaths; private Paint mStrokePaint; private Paint mFillPaint; + private ColorFilter mColorFilter; private PathMeasure mPathMeasure; private VGroup mCurrentGroup = new VGroup(); - float mBaseWidth = 1; - float mBaseHeight = 1; - float mViewportWidth; - float mViewportHeight; + float mBaseWidth = 0; + float mBaseHeight = 0; + float mViewportWidth = 0; + float mViewportHeight = 0; public VPathRenderer() { } @@ -413,6 +415,18 @@ public class VectorDrawable extends Drawable { } } + public void setColorFilter(ColorFilter colorFilter) { + mColorFilter = colorFilter; + + if (mFillPaint != null) { + mFillPaint.setColorFilter(colorFilter); + } + + if (mStrokePaint != null) { + mStrokePaint.setColorFilter(colorFilter); + } + } + public void draw(Canvas canvas, int w, int h) { if (mCurrentPaths == null) { Log.e(LOGTAG,"mCurrentPaths == null"); @@ -470,6 +484,7 @@ public class VectorDrawable extends Drawable { if (vPath.mFillColor != 0) { if (mFillPaint == null) { mFillPaint = new Paint(); + mFillPaint.setColorFilter(mColorFilter); mFillPaint.setStyle(Paint.Style.FILL); mFillPaint.setAntiAlias(true); } @@ -481,6 +496,7 @@ public class VectorDrawable extends Drawable { if (vPath.mStrokeColor != 0) { if (mStrokePaint == null) { mStrokePaint = new Paint(); + mStrokePaint.setColorFilter(mColorFilter); mStrokePaint.setStyle(Paint.Style.STROKE); mStrokePaint.setAntiAlias(true); } @@ -516,24 +532,34 @@ public class VectorDrawable extends Drawable { private void parseViewport(Resources r, AttributeSet attrs) throws XmlPullParserException { final TypedArray a = r.obtainAttributes(attrs, R.styleable.VectorDrawableViewport); - mViewportWidth = a.getFloat(R.styleable.VectorDrawableViewport_viewportWidth, 0); - mViewportHeight = a.getFloat(R.styleable.VectorDrawableViewport_viewportHeight, 0); - if (mViewportWidth == 0 || mViewportHeight == 0) { - throw new XmlPullParserException(a.getPositionDescription()+ - " tag requires viewportWidth & viewportHeight to be set"); + mViewportWidth = a.getFloat(R.styleable.VectorDrawableViewport_viewportWidth, mViewportWidth); + mViewportHeight = a.getFloat(R.styleable.VectorDrawableViewport_viewportHeight, mViewportHeight); + + if (mViewportWidth <= 0) { + throw new XmlPullParserException(a.getPositionDescription() + + " tag requires viewportWidth > 0"); + } else if (mViewportHeight <= 0) { + throw new XmlPullParserException(a.getPositionDescription() + + " tag requires viewportHeight > 0"); } + a.recycle(); } private void parseSize(Resources r, AttributeSet attrs) throws XmlPullParserException { final TypedArray a = r.obtainAttributes(attrs, R.styleable.VectorDrawableSize); - mBaseWidth = a.getDimension(R.styleable.VectorDrawableSize_width, 0); - mBaseHeight = a.getDimension(R.styleable.VectorDrawableSize_height, 0); - if (mBaseWidth == 0 || mBaseHeight == 0) { - throw new XmlPullParserException(a.getPositionDescription()+ - " tag requires width & height to be set"); + mBaseWidth = a.getDimension(R.styleable.VectorDrawableSize_width, mBaseWidth); + mBaseHeight = a.getDimension(R.styleable.VectorDrawableSize_height, mBaseHeight); + + if (mBaseWidth <= 0) { + throw new XmlPullParserException(a.getPositionDescription() + + " tag requires width > 0"); + } else if (mBaseHeight <= 0) { + throw new XmlPullParserException(a.getPositionDescription() + + " tag requires height > 0"); } + a.recycle(); } -- cgit v1.1