|
1 | 1 | package com.hjq.shape.builder; |
2 | 2 |
|
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.Configuration; |
| 5 | +import android.content.res.Resources; |
3 | 6 | import android.content.res.TypedArray; |
4 | 7 | import android.graphics.Color; |
5 | 8 | import android.graphics.drawable.Drawable; |
@@ -102,7 +105,7 @@ public ShapeDrawableBuilder(View view, TypedArray typedArray, IShapeDrawableStyl |
102 | 105 | mSolidSelectedColor = typedArray.getColor(styleable.getSolidSelectedColorStyleable(), NO_COLOR); |
103 | 106 | } |
104 | 107 |
|
105 | | - int layoutDirection = view.getLayoutDirection(); |
| 108 | + int layoutDirection = getLayoutDirection(view); |
106 | 109 |
|
107 | 110 | int radius = typedArray.getDimensionPixelSize(styleable.getRadiusStyleable(), 0); |
108 | 111 | mTopLeftRadius = mTopRightRadius = mBottomLeftRadius = mBottomRightRadius = radius; |
@@ -814,6 +817,28 @@ public void intoBackground() { |
814 | 817 | mView.setBackground(drawable); |
815 | 818 | } |
816 | 819 |
|
| 820 | + /** |
| 821 | + * 从上下文中获取当前布局方向 |
| 822 | + */ |
| 823 | + private static int getLayoutDirection(View view) { |
| 824 | + int layoutDirection; |
| 825 | + Context context = view.getContext(); |
| 826 | + Resources resources = null; |
| 827 | + Configuration configuration = null; |
| 828 | + if (context != null) { |
| 829 | + resources = context.getResources(); |
| 830 | + } |
| 831 | + if (resources != null) { |
| 832 | + configuration = resources.getConfiguration(); |
| 833 | + } |
| 834 | + if (configuration != null) { |
| 835 | + layoutDirection = configuration.getLayoutDirection(); |
| 836 | + } else { |
| 837 | + layoutDirection = View.LAYOUT_DIRECTION_LTR; |
| 838 | + } |
| 839 | + return layoutDirection; |
| 840 | + } |
| 841 | + |
817 | 842 | /** |
818 | 843 | * 将 ShapeView 框架中渐变色的 xml 属性值转换成 ShapeDrawable 中的枚举值 |
819 | 844 | */ |
|
0 commit comments