diff options
author | ztenghui <ztenghui@google.com> | 2014-07-16 11:17:56 -0700 |
---|---|---|
committer | Tenghui Zhu <ztenghui@google.com> | 2014-07-17 17:37:19 +0000 |
commit | 738177caf6a755a59ca6b17bb968be0aa4e8e10f (patch) | |
tree | d620da765a3fbe2a6246cdd0c63b83849e554c18 | |
parent | 580ff8142b7d0455d0d41ee77572b4f55dd935f0 (diff) | |
download | frameworks_base-738177caf6a755a59ca6b17bb968be0aa4e8e10f.zip frameworks_base-738177caf6a755a59ca6b17bb968be0aa4e8e10f.tar.gz frameworks_base-738177caf6a755a59ca6b17bb968be0aa4e8e10f.tar.bz2 |
Add the RTL support to VectorDrawable.
bug:15905631
Change-Id: Ieb3dcac2dd446ba89f307716411688dcd6ec5279
6 files changed, 42 insertions, 4 deletions
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index ab19ad4..f57d61d 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -5085,6 +5085,9 @@ <!-- When a tint color is set, specifies its Porter-Duff blending mode. The default value is src_in, which treats the drawable as an alpha mask. --> <attr name="tintMode" /> + <!-- Indicates if the drawable needs to be mirrored when its layout direction is + RTL (right-to-left). --> + <attr name="autoMirrored" /> </declare-styleable> <!-- Define the virtual size of the drawing surface paths will draw to. --> diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java index 8c907b2..43e6509 100644 --- a/graphics/java/android/graphics/drawable/VectorDrawable.java +++ b/graphics/java/android/graphics/drawable/VectorDrawable.java @@ -33,6 +33,7 @@ import android.graphics.Region; import android.graphics.PorterDuff.Mode; import android.util.ArrayMap; import android.util.AttributeSet; +import android.util.LayoutDirection; import android.util.Log; import android.util.PathParser; import android.util.Xml; @@ -187,7 +188,13 @@ public class VectorDrawable extends Drawable { public void draw(Canvas canvas) { final int saveCount = canvas.save(); final Rect bounds = getBounds(); + final boolean needMirroring = needMirroring(); + canvas.translate(bounds.left, bounds.top); + if (needMirroring) { + canvas.translate(bounds.width(), 0); + canvas.scale(-1.0f, 1.0f); + } if (!mAllowCaching) { mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height()); @@ -205,6 +212,7 @@ public class VectorDrawable extends Drawable { } canvas.drawBitmap(bitmap, null, bounds, null); } + canvas.restoreToCount(saveCount); } @@ -352,6 +360,9 @@ public class VectorDrawable extends Drawable { if (tint != null) { state.mTint = tint; } + + state.mAutoMirrored = a.getBoolean( + R.styleable.VectorDrawable_autoMirrored, state.mAutoMirrored); } private void inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs, @@ -469,12 +480,30 @@ public class VectorDrawable extends Drawable { mAllowCaching = allowCaching; } + private boolean needMirroring() { + return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL; + } + + @Override + public void setAutoMirrored(boolean mirrored) { + if (mVectorState.mAutoMirrored != mirrored) { + mVectorState.mAutoMirrored = mirrored; + invalidateSelf(); + } + } + + @Override + public boolean isAutoMirrored() { + return mVectorState.mAutoMirrored; + } + private static class VectorDrawableState extends ConstantState { int[] mThemeAttrs; int mChangingConfigurations; VPathRenderer mVPathRenderer; ColorStateList mTint; Mode mTintMode; + boolean mAutoMirrored; Bitmap mCachedBitmap; int[] mCachedThemeAttrs; @@ -490,6 +519,7 @@ public class VectorDrawable extends Drawable { mVPathRenderer = new VPathRenderer(copy.mVPathRenderer); mTint = copy.mTint; mTintMode = copy.mTintMode; + mAutoMirrored = copy.mAutoMirrored; } } @@ -497,6 +527,7 @@ public class VectorDrawable extends Drawable { if (mCachedThemeAttrs == mThemeAttrs && mCachedTint == mTint && mCachedTintMode == mTintMode + && mAutoMirrored == mAutoMirrored && width == mCachedBitmap.getWidth() && height == mCachedBitmap.getHeight() && mCachedRootAlpha == mVPathRenderer.getRootAlpha()) { diff --git a/tests/VectorDrawableTest/AndroidManifest.xml b/tests/VectorDrawableTest/AndroidManifest.xml index 56fa0a9..a16b749 100644 --- a/tests/VectorDrawableTest/AndroidManifest.xml +++ b/tests/VectorDrawableTest/AndroidManifest.xml @@ -22,7 +22,8 @@ <application android:hardwareAccelerated="true" - android:label="vector" > + android:label="vector" + android:supportsRtl="true" > <activity android:name="VectorDrawablePerformance" android:label="Vector Performance" > diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable03.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable03.xml index 5b4c4ab..2fdb676 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable03.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable03.xml @@ -13,7 +13,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<vector xmlns:android="http://schemas.android.com/apk/res/android" > +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:autoMirrored="true" > <size android:height="64dp" diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable04.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable04.xml index 90694fb..296e026 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable04.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable04.xml @@ -12,7 +12,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<vector xmlns:android="http://schemas.android.com/apk/res/android"> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:autoMirrored="true"> <size android:width="64dp" diff --git a/tests/VectorDrawableTest/res/drawable/vector_drawable05.xml b/tests/VectorDrawableTest/res/drawable/vector_drawable05.xml index c6595fa..1633326 100644 --- a/tests/VectorDrawableTest/res/drawable/vector_drawable05.xml +++ b/tests/VectorDrawableTest/res/drawable/vector_drawable05.xml @@ -13,7 +13,8 @@ See the License for the specific language governing permissions and limitations under the License. --> -<vector xmlns:android="http://schemas.android.com/apk/res/android" > +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:autoMirrored="true"> <size android:height="64dp" |