Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 738177ca authored by ztenghui's avatar ztenghui Committed by Tenghui Zhu
Browse files

Add the RTL support to VectorDrawable.

bug:15905631

Change-Id: Ieb3dcac2dd446ba89f307716411688dcd6ec5279
parent 580ff814
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -5085,6 +5085,9 @@
        <!-- When a tint color is set, specifies its Porter-Duff blending mode. The
        <!-- 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. -->
             default value is src_in, which treats the drawable as an alpha mask. -->
        <attr name="tintMode" />
        <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>
    </declare-styleable>


    <!-- Define the virtual size of the drawing surface paths will draw to. -->
    <!-- Define the virtual size of the drawing surface paths will draw to. -->
+31 −0
Original line number Original line Diff line number Diff line
@@ -33,6 +33,7 @@ import android.graphics.Region;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuff.Mode;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.util.AttributeSet;
import android.util.LayoutDirection;
import android.util.Log;
import android.util.Log;
import android.util.PathParser;
import android.util.PathParser;
import android.util.Xml;
import android.util.Xml;
@@ -187,7 +188,13 @@ public class VectorDrawable extends Drawable {
    public void draw(Canvas canvas) {
    public void draw(Canvas canvas) {
        final int saveCount = canvas.save();
        final int saveCount = canvas.save();
        final Rect bounds = getBounds();
        final Rect bounds = getBounds();
        final boolean needMirroring = needMirroring();

        canvas.translate(bounds.left, bounds.top);
        canvas.translate(bounds.left, bounds.top);
        if (needMirroring) {
            canvas.translate(bounds.width(), 0);
            canvas.scale(-1.0f, 1.0f);
        }


        if (!mAllowCaching) {
        if (!mAllowCaching) {
            mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height());
            mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height());
@@ -205,6 +212,7 @@ public class VectorDrawable extends Drawable {
            }
            }
            canvas.drawBitmap(bitmap, null, bounds, null);
            canvas.drawBitmap(bitmap, null, bounds, null);
        }
        }

        canvas.restoreToCount(saveCount);
        canvas.restoreToCount(saveCount);
    }
    }


@@ -352,6 +360,9 @@ public class VectorDrawable extends Drawable {
        if (tint != null) {
        if (tint != null) {
            state.mTint = tint;
            state.mTint = tint;
        }
        }

        state.mAutoMirrored = a.getBoolean(
                R.styleable.VectorDrawable_autoMirrored, state.mAutoMirrored);
    }
    }


    private void inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs,
    private void inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs,
@@ -469,12 +480,30 @@ public class VectorDrawable extends Drawable {
        mAllowCaching = allowCaching;
        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 {
    private static class VectorDrawableState extends ConstantState {
        int[] mThemeAttrs;
        int[] mThemeAttrs;
        int mChangingConfigurations;
        int mChangingConfigurations;
        VPathRenderer mVPathRenderer;
        VPathRenderer mVPathRenderer;
        ColorStateList mTint;
        ColorStateList mTint;
        Mode mTintMode;
        Mode mTintMode;
        boolean mAutoMirrored;


        Bitmap mCachedBitmap;
        Bitmap mCachedBitmap;
        int[] mCachedThemeAttrs;
        int[] mCachedThemeAttrs;
@@ -490,6 +519,7 @@ public class VectorDrawable extends Drawable {
                mVPathRenderer = new VPathRenderer(copy.mVPathRenderer);
                mVPathRenderer = new VPathRenderer(copy.mVPathRenderer);
                mTint = copy.mTint;
                mTint = copy.mTint;
                mTintMode = copy.mTintMode;
                mTintMode = copy.mTintMode;
                mAutoMirrored = copy.mAutoMirrored;
            }
            }
        }
        }


@@ -497,6 +527,7 @@ public class VectorDrawable extends Drawable {
            if (mCachedThemeAttrs == mThemeAttrs
            if (mCachedThemeAttrs == mThemeAttrs
                    && mCachedTint == mTint
                    && mCachedTint == mTint
                    && mCachedTintMode == mTintMode
                    && mCachedTintMode == mTintMode
                    && mAutoMirrored == mAutoMirrored
                    && width == mCachedBitmap.getWidth()
                    && width == mCachedBitmap.getWidth()
                    && height == mCachedBitmap.getHeight()
                    && height == mCachedBitmap.getHeight()
                    && mCachedRootAlpha == mVPathRenderer.getRootAlpha())  {
                    && mCachedRootAlpha == mVPathRenderer.getRootAlpha())  {
+2 −1
Original line number Original line Diff line number Diff line
@@ -22,7 +22,8 @@


    <application
    <application
        android:hardwareAccelerated="true"
        android:hardwareAccelerated="true"
        android:label="vector" >
        android:label="vector"
        android:supportsRtl="true" >
        <activity
        <activity
            android:name="VectorDrawablePerformance"
            android:name="VectorDrawablePerformance"
            android:label="Vector Performance" >
            android:label="Vector Performance" >
+2 −1
Original line number Original line Diff line number Diff line
@@ -13,7 +13,8 @@
     See the License for the specific language governing permissions and
     See the License for the specific language governing permissions and
     limitations under the License.
     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
    <size
        android:height="64dp"
        android:height="64dp"
+2 −1
Original line number Original line Diff line number Diff line
@@ -12,7 +12,8 @@
     See the License for the specific language governing permissions and
     See the License for the specific language governing permissions and
     limitations under the License.
     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
    <size
            android:width="64dp"
            android:width="64dp"
Loading