-
Notifications
You must be signed in to change notification settings - Fork 15
/
LoadingScreenManager.java
175 lines (140 loc) · 5.09 KB
/
LoadingScreenManager.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package company.tap.gosellapi.internal.data_managers;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import company.tap.gosellapi.R;
import gotap.com.tapglkitandroid.gl.Views.TapLoadingView;
import jp.wasabeef.blurry.Blurry;
/**
* The type Loading screen manager.
*/
public class LoadingScreenManager {
/**
* The interface Loading screen listener.
*/
public interface LoadingScreenListener {
/**
* On loading screen closed.
*/
void onLoadingScreenClosed();
}
// Constants
private static final int BLUR_RADIUS = 40;
private static final long ANIMATION_DURATION = 300;
// Variables
private AppCompatActivity hostActivity;
private static final int loadingLayoutID = R.layout.gosellapi_layout_loading;
private TapLoadingView loadingView;
private View loadingLayout;
private LoadingScreenListener listener;
private LoadingScreenManager() {
}
private static class SingletonHolder {
private static final LoadingScreenManager INSTANCE = new LoadingScreenManager();
}
/**
* Gets instance.
*
* @return the instance
*/
public static LoadingScreenManager getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* Show loading screen.
*
* @param activity the activity
*/
public void showLoadingScreen(AppCompatActivity activity) {
this.hostActivity = activity;
createLoadingScreen();
}
/**
* Close loading screen.
*/
public void closeLoadingScreen() {
removeLoadingView();
}
/**
* Close loading screen with listener.
*
* @param listener the listener
*/
public void closeLoadingScreenWithListener(LoadingScreenListener listener) {
this.listener = listener;
removeLoadingView();
}
private void removeLoadingView() {
// Log.d("LoadingScreenManager","removeLoadingView : "+loadingLayout +" >> loadingView = "+loadingView );
if (loadingLayout == null ) return;
final ViewGroup insertPoint = hostActivity.findViewById(android.R.id.content);
if(loadingView!=null ) loadingView.setForceStop(true);
else return;
loadingLayout.animate()
.alpha(0.0f)
.setDuration(ANIMATION_DURATION)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
insertPoint.removeView(loadingLayout);
loadingLayout = null;
hostActivity = null;
if(listener != null) {
listener.onLoadingScreenClosed();
listener = null;
}
}
});
}
private void createLoadingScreen() {
LayoutInflater inflater = (LayoutInflater) hostActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (inflater == null) return;
loadingLayout = inflater.inflate(loadingLayoutID, null);
loadingLayout.setAlpha(0.0f);
// Configure background
ImageView loadingViewBackground = loadingLayout.findViewById(R.id.blurredBackground);
ViewGroup insertPoint = hostActivity.findViewById(android.R.id.content);
Bitmap bitmap = createScreenshot(insertPoint);
Blurry.with(hostActivity)
.radius(BLUR_RADIUS)
.color(Color.argb(220, 255, 255, 255))
.from(bitmap)
.into(loadingViewBackground);
insertPoint.addView(loadingLayout, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
loadingLayout.bringToFront();
// Configure appearance animation
loadingLayout.animate()
.alpha(1.0f)
.setDuration(ANIMATION_DURATION)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// Configure loading view
loadingView = loadingLayout.findViewById(R.id.loadingView);
loadingView.start();
}
});
}
private Bitmap createScreenshot(ViewGroup view) {
int w = view.getWidth();
int h = view.getHeight();
if(w<=0 )
w=800;
if(h<=0 )
h=300;
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
}