• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package io.flutter.embedding.android;
6 
7 import android.animation.Animator;
8 import android.content.Context;
9 import android.graphics.drawable.Drawable;
10 import android.os.Bundle;
11 import android.support.annotation.NonNull;
12 import android.support.annotation.Nullable;
13 import android.util.AttributeSet;
14 import android.view.View;
15 import android.widget.ImageView;
16 
17 /**
18  * {@link SplashScreen} that displays a given {@link Drawable}, which then fades its alpha
19  * to zero when instructed to {@link #transitionToFlutter(Runnable)}.
20  */
21 public final class DrawableSplashScreen implements SplashScreen {
22   private final Drawable drawable;
23   private final ImageView.ScaleType scaleType;
24   private final long crossfadeDurationInMillis;
25   private DrawableSplashScreenView splashView;
26 
27   /**
28    * Constructs a {@code DrawableSplashScreen} that displays the given {@code drawable} and
29    * crossfades to Flutter content in 500 milliseconds.
30    */
DrawableSplashScreen(@onNull Drawable drawable)31   public DrawableSplashScreen(@NonNull Drawable drawable) {
32     this(drawable, ImageView.ScaleType.FIT_XY, 500);
33   }
34 
35   /**
36    * Constructs a {@code DrawableSplashScreen} that displays the given {@code drawable} and
37    * crossfades to Flutter content in the given {@code crossfadeDurationInMillis}.
38    * <p>
39    * @param drawable The {@code Drawable} to be displayed as a splash screen.
40    * @param scaleType The {@link ImageView.ScaleType} to be applied to the {@code Drawable} when the
41    *                  {@code Drawable} is displayed full-screen.
42    */
DrawableSplashScreen(@onNull Drawable drawable, @NonNull ImageView.ScaleType scaleType, long crossfadeDurationInMillis)43   public DrawableSplashScreen(@NonNull Drawable drawable, @NonNull ImageView.ScaleType scaleType, long crossfadeDurationInMillis) {
44     this.drawable = drawable;
45     this.scaleType = scaleType;
46     this.crossfadeDurationInMillis = crossfadeDurationInMillis;
47   }
48 
49   @Nullable
50   @Override
createSplashView(@onNull Context context, @Nullable Bundle savedInstanceState)51   public View createSplashView(@NonNull Context context, @Nullable Bundle savedInstanceState) {
52     splashView = new DrawableSplashScreenView(context);
53     splashView.setSplashDrawable(drawable, scaleType);
54     return splashView;
55   }
56 
57   @Override
transitionToFlutter(@onNull Runnable onTransitionComplete)58   public void transitionToFlutter(@NonNull Runnable onTransitionComplete) {
59     if (splashView == null) {
60       onTransitionComplete.run();
61       return;
62     }
63 
64     splashView.animate()
65         .alpha(0.0f)
66         .setDuration(crossfadeDurationInMillis)
67         .setListener(new Animator.AnimatorListener() {
68           @Override
69           public void onAnimationStart(Animator animation) {}
70 
71           @Override
72           public void onAnimationEnd(Animator animation) {
73             onTransitionComplete.run();
74           }
75 
76           @Override
77           public void onAnimationCancel(Animator animation) {
78             onTransitionComplete.run();
79           }
80 
81           @Override
82           public void onAnimationRepeat(Animator animation) {}
83         }
84     );
85   }
86 
87   // Public for Android OS requirements. This View should not be used by external developers.
88   public static class DrawableSplashScreenView extends ImageView {
DrawableSplashScreenView(@onNull Context context)89     public DrawableSplashScreenView(@NonNull Context context) {
90       this(context, null, 0);
91     }
92 
DrawableSplashScreenView(@onNull Context context, @Nullable AttributeSet attrs)93     public DrawableSplashScreenView(@NonNull Context context, @Nullable AttributeSet attrs) {
94       this(context, attrs, 0);
95     }
96 
DrawableSplashScreenView(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)97     public DrawableSplashScreenView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
98       super(context, attrs, defStyleAttr);
99     }
100 
setSplashDrawable(@ullable Drawable drawable)101     public void setSplashDrawable(@Nullable Drawable drawable) {
102       setSplashDrawable(drawable, ImageView.ScaleType.FIT_XY);
103     }
104 
setSplashDrawable(@ullable Drawable drawable, @NonNull ImageView.ScaleType scaleType)105     public void setSplashDrawable(@Nullable Drawable drawable, @NonNull ImageView.ScaleType scaleType) {
106       setScaleType(scaleType);
107       setImageDrawable(drawable);
108     }
109   }
110 }
111