-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathRCTSplashScreen.java
More file actions
148 lines (121 loc) · 5.84 KB
/
RCTSplashScreen.java
File metadata and controls
148 lines (121 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.reactnativecomponent.splashscreen;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.lang.ref.WeakReference;
import pl.droidsonroids.gif.GifImageView;
public class RCTSplashScreen {
public static final int UIAnimationNone = 0;
public static final int UIAnimationFade = 1;
public static final int UIAnimationScale = 2;
private static Dialog dialog;
private static GifImageView imageView;
private static WeakReference<Activity> wr_activity;
protected static Activity getActivity() {
return wr_activity.get();
}
public static void openSplashScreen(Activity activity) {
openSplashScreen(activity, false);
}
public static void openSplashScreen(Activity activity, boolean isFullScreen) {
openSplashScreen(activity, isFullScreen, GifImageView.ScaleType.CENTER_CROP);
}
public static void openSplashScreen(final Activity activity, final boolean isFullScreen, final GifImageView.ScaleType scaleType) {
if (activity == null) return;
wr_activity = new WeakReference<>(activity);
final int drawableId = getImageId();
if ((dialog != null && dialog.isShowing())||(drawableId == 0)) {
return;
}
activity.runOnUiThread(new Runnable() {
public void run() {
if(!getActivity().isFinishing()) {
Context context = getActivity();
imageView = new GifImageView(context);
imageView.setImageResource(drawableId);
LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(layoutParams);
imageView.setImageResource(drawableId);
imageView.setScaleType(scaleType);
dialog = new Dialog(context, isFullScreen ? android.R.style.Theme_Translucent_NoTitleBar_Fullscreen : android.R.style.Theme_Translucent_NoTitleBar);
// if ((getActivity().getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
// == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
// dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
// }
dialog.setContentView(imageView);
dialog.setCancelable(false);
dialog.show();
}
}
});
}
public static void removeSplashScreen(Activity activity, final int animationType,final int duration) {
if (activity == null) {
activity = getActivity();
if(activity == null) return;
}
activity.runOnUiThread(new Runnable() {
public void run() {
if (dialog != null && dialog.isShowing()) {
AnimationSet animationSet = new AnimationSet(true);
if(animationType == UIAnimationScale) {
AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setDuration(duration);
animationSet.addAnimation(fadeOut);
ScaleAnimation scale = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.65f);
scale.setDuration(duration);
animationSet.addAnimation(scale);
}
else if(animationType == UIAnimationFade) {
AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setDuration(duration);
animationSet.addAnimation(fadeOut);
}
else {
AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setDuration(0);
animationSet.addAnimation(fadeOut);
}
final View view = ((ViewGroup)dialog.getWindow().getDecorView()).getChildAt(0);
view.startAnimation(animationSet);
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.post(new Runnable() {
@Override
public void run() {
dialog.dismiss();
dialog = null;
imageView = null;
}
});
}
});
}
}
});
}
private static int getImageId() {
int drawableId = getActivity().getResources().getIdentifier("splash", "drawable", getActivity().getClass().getPackage().getName());
if (drawableId == 0) {
drawableId = getActivity().getResources().getIdentifier("splash", "drawable", getActivity().getPackageName());
}
return drawableId;
}
}