2323import android .graphics .ColorFilter ;
2424import android .graphics .PixelFormat ;
2525import android .graphics .drawable .Drawable ;
26+ import android .util .LayoutDirection ;
2627import androidx .annotation .FloatRange ;
2728import androidx .annotation .NonNull ;
2829import androidx .annotation .Nullable ;
@@ -43,13 +44,25 @@ public class FadeThroughDrawable extends Drawable {
4344
4445 private float progress ;
4546
46- public FadeThroughDrawable (@ NonNull Drawable fadeOutDrawable , @ NonNull Drawable fadeInDrawable ) {
47- this .fadeOutDrawable = fadeOutDrawable .getConstantState ().newDrawable ().mutate ();
48- this .fadeInDrawable = fadeInDrawable .getConstantState ().newDrawable ().mutate ();
49- DrawableCompat .setLayoutDirection (
50- this .fadeOutDrawable , DrawableCompat .getLayoutDirection (fadeOutDrawable ));
51- DrawableCompat .setLayoutDirection (
52- this .fadeInDrawable , DrawableCompat .getLayoutDirection (fadeInDrawable ));
47+ public FadeThroughDrawable (@ Nullable Drawable fadeOutDrawable , @ Nullable Drawable fadeInDrawable ) {
48+ this .fadeOutDrawable =
49+ fadeOutDrawable != null
50+ ? fadeOutDrawable .getConstantState ().newDrawable ().mutate ()
51+ : new EmptyDrawable ();
52+ this .fadeInDrawable =
53+ fadeInDrawable != null
54+ ? fadeInDrawable .getConstantState ().newDrawable ().mutate ()
55+ : new EmptyDrawable ();
56+ int outLayoutDir =
57+ fadeOutDrawable != null
58+ ? DrawableCompat .getLayoutDirection (fadeOutDrawable )
59+ : LayoutDirection .LOCALE ;
60+ int inLayoutDir =
61+ fadeInDrawable != null
62+ ? DrawableCompat .getLayoutDirection (fadeInDrawable )
63+ : LayoutDirection .LOCALE ;
64+ DrawableCompat .setLayoutDirection (this .fadeOutDrawable , outLayoutDir );
65+ DrawableCompat .setLayoutDirection (this .fadeInDrawable , inLayoutDir );
5366 this .fadeInDrawable .setAlpha (0 );
5467 this .alphas = new float [2 ];
5568 }
@@ -135,4 +148,21 @@ public void setProgress(@FloatRange(from = 0.0, to = 1.0) float progress) {
135148 invalidateSelf ();
136149 }
137150 }
151+
152+ private static class EmptyDrawable extends Drawable {
153+
154+ @ Override
155+ public void draw (@ NonNull Canvas canvas ) {}
156+
157+ @ Override
158+ public void setAlpha (int alpha ) {}
159+
160+ @ Override
161+ public void setColorFilter (@ Nullable ColorFilter colorFilter ) {}
162+
163+ @ Override
164+ public int getOpacity () {
165+ return PixelFormat .TRANSPARENT ;
166+ }
167+ }
138168}
0 commit comments