EasterEggsDialog.java
6.08 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.wd.common.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.wd.foundation.wdkit.constant.Constants;
import com.wd.common.imageglide.ImageUtils;
import com.wd.common.viewclick.BaseClickListener;
import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkit.animator.MyAnimationUtils;
import com.wd.foundation.wdkit.utils.UiUtils;
/**
* @author baozhaoxin
* @version [V1.0.0, 2022/8/17]
* @description 彩蛋弹窗
* @since V1.0.0
*/
public class EasterEggsDialog {
/**
* 上下文环境
*/
private Context context;
/**
* dialog
*/
private Dialog dialog;
private Display display;
/**
* 弹窗布局
*/
private RelativeLayout dialogLaout;
/**
* 弹窗图片
*/
private ImageView img;
/**
* 弹窗关闭按钮
*/
private ImageView closeImg;
/**
* 监听
*/
private DialogClickListener clickListener;
public View view;
public EasterEggsDialog(Context context) {
this.context = context;
display = UiUtils.getDisplay(context);
}
public Dialog getDialog() {
return dialog;
}
/**
* 创建布局
* @return
*/
public EasterEggsDialog builder() {
// 获取Dialog布局
view = LayoutInflater.from(context).inflate(R.layout.dialog_easter_eggs, null);
dialogLaout = view.findViewById(R.id.dialog_layout);
img = view.findViewById(R.id.img);
closeImg = view.findViewById(R.id.img_close);
// 定义Dialog布局和参数
dialog = new Dialog(context, R.style.AlertDialogStyle);
dialog.setContentView(view);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.5f;
// lp.y = 250;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// 调整dialog背景大小
dialogLaout.setLayoutParams(
new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//点击空白区域,更新状态
Constants.isShowingEasterEggs = false;
}
});
Animation animation = MyAnimationUtils.loadAnimation(context, R.anim.scale_easter_eggs_dialog);
view.setAnimation(animation);
animation.start();
img.setOnClickListener(new BaseClickListener() {
@Override
protected void onNoDoubleClick(View v) {
if(clickListener!=null){
clickListener.onJump();
}
dialog.dismiss();
}
});
closeImg.setOnClickListener(view1 -> {
if(clickListener!=null){
clickListener.onClose();
}
dialog.dismiss();
});
return this;
}
/**
* 设置监听
* @param clickListener
*/
public EasterEggsDialog setClickListener(DialogClickListener clickListener){
this.clickListener = clickListener;
return this;
}
/**
* 设置彩蛋素材地址
* @param materialUrl
* @return
*/
public EasterEggsDialog setMaterialUrl(String materialUrl){
if(img!=null){
ImageUtils.getInstance().loadImage(img,materialUrl);
}
return this;
}
/**
* 自动关闭时长: 0代表不自动关闭,其他为对应时长(s)
* @param durations
* @return
*/
public EasterEggsDialog setDurations(String durations){
try {
int duration = Integer.parseInt(durations);
if(duration>0){
long mDuration = duration*1000;
//自动关闭
new Handler(Looper.myLooper()).postDelayed(() -> {
if(dialog!=null){
Constants.isShowingEasterEggs = false;
dialog.dismiss();
}
},mDuration);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return this;
}
public boolean isShowing() {
if (dialog!=null){
return dialog.isShowing();
}
return false;
}
/**
* 回调监听
*/
public interface DialogClickListener {
void onJump();
void onClose();
}
/**
* 显示
*/
public EasterEggsDialog show() {
if(context != null){
if(context instanceof Activity){
Activity activity = (Activity) context;
if(!activity.isDestroyed() && !activity.isFinishing()){
if(dialog!=null){
dialog.show();
}
}
}else{
if(dialog!=null){
dialog.show();
}
}
}
return this;
}
/**
* 关闭
*/
public EasterEggsDialog close() {
Constants.isShowingEasterEggs = false;
if(context != null){
if(context instanceof Activity){
Activity activity = (Activity) context;
if(!activity.isDestroyed() && !activity.isFinishing()){
if(dialog!=null){
dialog.dismiss();
}
}
}else{
if(dialog!=null){
dialog.dismiss();
}
}
}
return this;
}
}