11package com .hjq .shape .builder ;
22
3+ import android .content .Context ;
34import android .content .res .ColorStateList ;
5+ import android .content .res .Configuration ;
6+ import android .content .res .Resources ;
47import android .content .res .TypedArray ;
58import android .graphics .Canvas ;
69import android .graphics .Color ;
1114import android .support .annotation .Nullable ;
1215import android .text .SpannableStringBuilder ;
1316import android .text .Spanned ;
17+ import android .view .View ;
1418import android .widget .LinearLayout ;
1519import android .widget .TextView ;
1620import android .widget .TextView .BufferType ;
@@ -317,21 +321,16 @@ public void intoTextColor() {
317321 mTextView .postInvalidate ();
318322 }
319323
320- public void onDraw (@ NonNull Canvas canvas , Paint paint ) {
324+ public void onDraw (@ NonNull View view , @ NonNull Canvas canvas , Paint paint ) {
321325 if (isTextGradientColorsEnable ()) {
322- LinearGradient linearGradient ;
323- if (mTextGradientOrientation == GRADIENT_ORIENTATION_VERTICAL ) {
324- linearGradient = new LinearGradient (
325- mTextView .getPaddingLeft (), mTextView .getPaddingTop (), 0 ,
326- (float ) canvas .getHeight () - mTextView .getPaddingBottom (),
327- mTextGradientColors , null , Shader .TileMode .CLAMP );
326+ int [] textGradientColors ;
327+ if (mTextGradientOrientation == GRADIENT_ORIENTATION_HORIZONTAL &&
328+ getLayoutDirectionByContext (view .getContext ()) == View .LAYOUT_DIRECTION_RTL ) {
329+ textGradientColors = reverseArray (mTextGradientColors );
328330 } else {
329- linearGradient = new LinearGradient (
330- mTextView .getPaddingLeft (), mTextView .getPaddingTop (),
331- (float ) canvas .getWidth () - mTextView .getPaddingEnd (),
332- (float ) canvas .getHeight () - mTextView .getPaddingBottom (),
333- mTextGradientColors , null , Shader .TileMode .CLAMP );
331+ textGradientColors = mTextGradientColors ;
334332 }
333+ LinearGradient linearGradient = getLinearGradient (view , canvas , mTextGradientOrientation , textGradientColors );
335334 paint .setShader (linearGradient );
336335 } else {
337336 Shader shader = paint .getShader ();
@@ -340,4 +339,59 @@ public void onDraw(@NonNull Canvas canvas, Paint paint) {
340339 }
341340 }
342341 }
342+
343+ /**
344+ * 获取线性渐变对象
345+ */
346+ private static LinearGradient getLinearGradient (@ NonNull View view , @ NonNull Canvas canvas ,
347+ int textGradientOrientation ,
348+ @ Nullable int [] textGradientColors ) {
349+ LinearGradient linearGradient ;
350+ if (textGradientOrientation == GRADIENT_ORIENTATION_VERTICAL ) {
351+ linearGradient = new LinearGradient (
352+ view .getPaddingLeft (), view .getPaddingTop (), 0 ,
353+ (float ) canvas .getHeight () - view .getPaddingBottom (),
354+ textGradientColors , null , Shader .TileMode .CLAMP );
355+ } else {
356+ linearGradient = new LinearGradient (
357+ view .getPaddingLeft (), view .getPaddingTop (),
358+ (float ) canvas .getWidth () - view .getPaddingEnd (),
359+ (float ) canvas .getHeight () - view .getPaddingBottom (),
360+ textGradientColors , null , Shader .TileMode .CLAMP );
361+ }
362+ return linearGradient ;
363+ }
364+
365+ /**
366+ * 从 Context 中获取当前布局方向
367+ */
368+ private static int getLayoutDirectionByContext (@ Nullable Context context ) {
369+ int layoutDirection ;
370+ Resources resources = null ;
371+ Configuration configuration = null ;
372+ if (context != null ) {
373+ resources = context .getResources ();
374+ }
375+ if (resources != null ) {
376+ configuration = resources .getConfiguration ();
377+ }
378+ if (configuration != null ) {
379+ layoutDirection = configuration .getLayoutDirection ();
380+ } else {
381+ layoutDirection = View .LAYOUT_DIRECTION_LTR ;
382+ }
383+ return layoutDirection ;
384+ }
385+
386+ /**
387+ * 反转 int 数组
388+ */
389+ public static int [] reverseArray (@ NonNull int [] originalArray ) {
390+ int length = originalArray .length ;
391+ int [] newArray = new int [length ];
392+ for (int i = 0 ; i < length ; i ++) {
393+ newArray [i ] = originalArray [length - 1 - i ];
394+ }
395+ return newArray ;
396+ }
343397}
0 commit comments