-
Notifications
You must be signed in to change notification settings - Fork 242
/
SpotsDialog.java
169 lines (139 loc) · 5 KB
/
SpotsDialog.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
package dmax.dialog;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.TextView;
/**
* Created by Maxim Dybarsky | maxim.dybarskyy@gmail.com
* on 13.01.15 at 14:22
*/
public class SpotsDialog extends AlertDialog {
public static class Builder {
private Context context;
private String message;
private int messageId;
private int themeId;
private boolean cancelable = true; // default dialog behaviour
private OnCancelListener cancelListener;
public Builder setContext(Context context) {
this.context = context;
return this;
}
public Builder setMessage(String message) {
this.message = message;
return this;
}
public Builder setMessage(@StringRes int messageId) {
this.messageId = messageId;
return this;
}
public Builder setTheme(@StyleRes int themeId) {
this.themeId = themeId;
return this;
}
public Builder setCancelable(boolean cancelable) {
this.cancelable = cancelable;
return this;
}
public Builder setCancelListener(OnCancelListener cancelListener) {
this.cancelListener = cancelListener;
return this;
}
public AlertDialog build() {
return new SpotsDialog(
context,
messageId != 0 ? context.getString(messageId) : message,
themeId != 0 ? themeId : R.style.SpotsDialogDefault,
cancelable,
cancelListener
);
}
}
private static final int DELAY = 150;
private static final int DURATION = 1500;
private int size;
private AnimatedView[] spots;
private AnimatorPlayer animator;
private CharSequence message;
private SpotsDialog(Context context, String message, int theme, boolean cancelable, OnCancelListener cancelListener) {
super(context, theme);
this.message = message;
setCancelable(cancelable);
if (cancelListener != null) setOnCancelListener(cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dmax_spots_dialog);
setCanceledOnTouchOutside(false);
initMessage();
initProgress();
}
@Override
protected void onStart() {
super.onStart();
for (AnimatedView view : spots) view.setVisibility(View.VISIBLE);
animator = new AnimatorPlayer(createAnimations());
animator.play();
}
@Override
protected void onStop() {
super.onStop();
animator.stop();
}
@Override
public void setMessage(CharSequence message) {
this.message = message;
if (isShowing()) initMessage();
}
//~
private void initMessage() {
if (message != null && message.length() > 0) {
((TextView) findViewById(R.id.dmax_spots_title)).setText(message);
}
}
private void initProgress() {
ProgressLayout progress = findViewById(R.id.dmax_spots_progress);
size = progress.getSpotsCount();
spots = new AnimatedView[size];
int size = getContext().getResources().getDimensionPixelSize(R.dimen.spot_size);
int progressWidth = getContext().getResources().getDimensionPixelSize(R.dimen.progress_width);
for (int i = 0; i < spots.length; i++) {
AnimatedView v = new AnimatedView(getContext());
v.setBackgroundResource(R.drawable.dmax_spots_spot);
v.setTarget(progressWidth);
v.setXFactor(-1f);
v.setVisibility(View.INVISIBLE);
progress.addView(v, size, size);
spots[i] = v;
}
}
private Animator[] createAnimations() {
Animator[] animators = new Animator[size];
for (int i = 0; i < spots.length; i++) {
final AnimatedView animatedView = spots[i];
Animator move = ObjectAnimator.ofFloat(animatedView, "xFactor", 0, 1);
move.setDuration(DURATION);
move.setInterpolator(new HesitateInterpolator());
move.setStartDelay(DELAY * i);
move.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animatedView.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationStart(Animator animation) {
animatedView.setVisibility(View.VISIBLE);
}
});
animators[i] = move;
}
return animators;
}
}