RTL 支持以下 API 19 的矢量可绘制对象

RTL support for vector drawable for below API 19

我有一个 vector 可绘制对象。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
    <path
        android:fillColor="#65666a"
        android:pathData="M8.59,16.34l4.58,-4.59 -4.58,-4.59L10,5.75l6,6 -6,6z" />
</vector>

我添加了 android:autoMirrored="true" 属性来支持 RTL,但它只在 API 19 及更高版本中使用。但我的最低 API 水平是 API 17. 如何添加向后可比性?任何帮助将不胜感激。

这是我的做法,而不是使用 autoMirrored,使用此变通方法来做你自己的 "automirror"。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <group
        android:pivotX="12"
        android:scaleX="@dimen/rtl_automirror_scale">
        <path
            android:fillColor="#65666a"
            android:pathData="M8.59,16.34l4.58,-4.59 -4.58,-4.59L10,5.75l6,6 -6,6z" />
    </group>
</vector>

我在这里删除了 autoMirror 属性,而是将矢量路径包裹在一个 <group> 标记中,其中 pivotX 点为 12(即可绘制对象的中间)和一个 scaleX 指向 Dimen 资源。

在您的正常值文件夹中,您可以将资源提供为:

<resources>

    <item name="rtl_automirror_scale" format="float" type="dimen">1</item>

</resources>

缩放值为 1 表示没有变化,只是正常的可绘制对象。

但是您可以为 values-ldrtl 中的 RTL 设备提供替代值资源:

<resources>

    <item name="rtl_automirror_scale" format="float" type="dimen">-1</item>

</resources>

值 -1 表示 VectorDrawable 将水平翻转 RTL 设备。

然后当你有其他VectorDrawables需要翻转时,只需将它们包裹在上面的组标签中,指向相同的维度即可。

在我的图像视图中我添加了

android:scaleX="@integer/local_mirror_scale_x"

然后在 integers.xml 中的值

英文整数

<integer name="local_mirror_scale_x">1</integer>

阿拉伯整数

<integer name="local_mirror_scale_x">-1</integer>