问题
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?
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:
Nexus 7 with Android 5.0:
Thanks
回答1:
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;
}
回答2:
you can use create custom ImageView and if persian rotate 180.
来源:https://stackoverflow.com/questions/38702131/auto-mirroring-for-rtl-layout-doesnt-work-in-android-versions-below-6-0