Auto Mirroring for RTL layout doesn't work in

2019-06-22 14:21发布

As you know Vector Drawables was added in Android Support Library 23.2 which announced in Android Developer Blog that for all versions of android we can use it instead of adding extra icons in different sizes. However "enable auto mirroring for RTL layout" option doesn't work in android versions below 6.0! Is there any additional setting to use it in other android versions?

enter image description here

My Test Project uses a simple method to changing the Locale of my application. These are results of my test:

Nexus 6P with Android 6.0 which works nice:

enter image description here enter image description here

Nexus 7 with Android 5.0:

enter image description here enter image description here

Thanks

2条回答
倾城 Initia
2楼-- · 2019-06-22 14:27

Bug Reported: link

Flip vector drawable if local is arabic and drawable is auto mirror

public static Drawable getDrawableLocale(Activity activity, @DrawableRes int drawableResId) {
        if (!Util.isRTL() || !ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_back_white, null).isAutoMirrored())
            return ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_back_white, null);
        /**
         * Flip it for RTl because Kitkat doesn't flip
         */
        Bitmap bitmap = Util.getBitmapFromVectorDrawable(activity, drawableResId);
        Matrix matrix = new Matrix();
        matrix.preScale(-1.0f, 1.0f);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return new BitmapDrawable(activity.getResources(), bitmap);
    }

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = getVectorDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
public static Drawable getVectorDrawable(Context context, @DrawableRes int idVectorDrawable) {
        return AppCompatDrawableManager.get().getDrawable(context, idVectorDrawable);
    }


public static boolean isRTL() {
        return isRTL(Locale.getDefault());
    }
public static boolean isRTL(Locale locale) {
    final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
    return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
            directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
}
查看更多
放荡不羁爱自由
3楼-- · 2019-06-22 14:35

you can use create custom ImageView and if persian rotate 180.

查看更多
登录 后发表回答