DialogUtil.java
16.6 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.cnlive.goldenline.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.api.CmsAPI;
import com.cnlive.goldenline.api.UserAPI;
import com.cnlive.goldenline.model.AwardBean;
import com.cnlive.goldenline.model.EventType;
import com.cnlive.goldenline.model.mine.UpdateGoldBalanceBean;
import com.cnlive.goldenline.model.mine.UpdateGoldRequestBean;
import com.cnlive.goldenline.model.request.AwardRequest;
import com.cnlive.goldenline.ui.activity.WalletActivity;
import org.greenrobot.eventbus.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* 对话框工具类
*/
@SuppressLint("InflateParams")
public class DialogUtil {
private static UserService mUserService;
/**
* 弹框时设置背景 阴影度 50%
*/
private static final float WINDOW_ALPHA_DARK = 0.5f;
private static TextView[] textViews;
public static Dialog portraitDialog;//竖屏
public static Dialog landscapeDialog;//横屏
private static void setAlpha(Activity context, float alpha) {
LayoutParams params = context.getWindow().getAttributes();
params.alpha = alpha;
context.getWindow().setAttributes(params);
}
/**
* 显示dialog
*
* @param view
*/
private static void showDialog(Dialog dialog, View view, int gravity, boolean cancelable) {
Window window = dialog.getWindow();
window.setGravity(gravity); // 此处可以设置dialog显示的位置
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
dialog.setContentView(view);
dialog.setCancelable(cancelable);
dialog.setCanceledOnTouchOutside(cancelable);
dialog.show();
}
// private static String[] number = {"1", "2", "5", "10", "15", "20"};
private static String[] number = {"1", "2", "5", "10", "15", "20"};
private static int[] res_back = {R.drawable.bg_dashang, R.drawable.bg_dashang_press};
private static int[] res_landscape_back = {R.drawable.bg_landscape_dashang, R.drawable.bg_landscape_dashang_press};
/**
* @param context
* @param title 标题
* @param url 头像url
* @param yue 余额
* @param right 没用
* @param key 暂时没用
*/
public static void showPlaytour(final Activity context, final String videoId, String title, final String url, String yue, String right, final String key) {
portraitDialog = new Dialog(context, R.style.NoTitleDialog);
//默认1元
final String[] key_choose = {"1"};
setAlpha(context, 50);
final View[] view = {LayoutInflater.from(context).inflate(R.layout.layout_dialog_dashang, null)};
final ImageView[] ima_diahead = {(ImageView) view[0].findViewById(R.id.ima_diahead)};
ImageButton imbtn_colse = (ImageButton) view[0].findViewById(R.id.imbtn_colse);
TextView tv_dianame = (TextView) view[0].findViewById(R.id.tv_dianame);
TextView text_pay_1 = (TextView) view[0].findViewById(R.id.text_pay_1);
TextView text_pay_2 = (TextView) view[0].findViewById(R.id.text_pay_2);
TextView text_pay_5 = (TextView) view[0].findViewById(R.id.text_pay_5);
TextView text_pay_10 = (TextView) view[0].findViewById(R.id.text_pay_10);
TextView text_pay_15 = (TextView) view[0].findViewById(R.id.text_pay_15);
TextView text_pay_20 = (TextView) view[0].findViewById(R.id.text_pay_20);
Button button_pay = (Button) view[0].findViewById(R.id.button_pay);
TextView tv_yue = (TextView) view[0].findViewById(R.id.tv_yue);
TextView tv_button_recharge = (TextView) view[0].findViewById(R.id.tv_button_recharge);
textViews = new TextView[]{text_pay_1, text_pay_2, text_pay_5, text_pay_10, text_pay_15, text_pay_20};
setTextColorSize(context, 0);//默认选中第一个
//Glide.with(context).load(url).error(R.mipmap.ic_launcher).transform(new GlideRoundTransform(context)).fitCenter().into(ima_diahead[0]);
Glide.with(context).load(url).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL)
.transform(new CircleTransform(context))
.placeholder(R.mipmap.find_default_theatre)
.error(R.mipmap.find_default_theatre)
.crossFade()
.into(ima_diahead[0]);
if(title!=null){
tv_dianame.setText(title);
}else{
tv_dianame.setText("正在上演推荐");
}
tv_yue.setText("金币余额:" + yue);
for (int i = 0; i < textViews.length; i++) {
final int finalI = i;
textViews[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setTextColorSize(context, finalI);
key_choose[0] = number[finalI];
// ToastUtil.showToast("充值金额=="+number[finalI]);
}
});
}
imbtn_colse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
portraitDialog.dismiss();
}
});
mUserService = new UserService(context);
//打赏
button_pay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RestAdapterUtils.getRestAPI(CmsAPI.class).getAward(new AwardRequest(mUserService.getUserInfo().getCn_uid(), mUserService.getUserInfo().getNickname(), key_choose[0], "4", videoId, mUserService.getUserInfo().getAvatar()), new Callback<AwardBean>() {
@Override
public void success(AwardBean awardBean, Response response) {
if ("0".equals(awardBean.getCode())) {
ToastUtil.showToast("您已打赏成功,感谢您的支持!");
portraitDialog.dismiss();
updataUserGold();
EventBus.getDefault().post(new EventType(EventType.EVENT_REFRESH));
} else if ("5".equals(awardBean.getCode())) {
ToastUtil.showToast("金币不足 请充值.");
WalletActivity.start(context);
portraitDialog.dismiss();
} else {
ToastUtil.showToast("打赏失败");
}
}
@Override
public void failure(RetrofitError error) {
if (NetworkUtils.isNetworkAvaliable(context)) {
ToastUtil.showToast("打赏失败");
} else {
ToastUtil.showToast( "网络跑路了,请连接网络");
}
}
});
//ToastUtil.showToast("充值金额=="+key_choose[0]);
}
});
//充值
tv_button_recharge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//EventBus.getDefault().post("button_pay");
//跳转充值页面
if (NetworkUtils.isNetworkAvaliable(context)) {
WalletActivity.start(context);
portraitDialog.dismiss();
} else {
ToastUtil.showToast( "网络跑路了,请连接网络");
}
}
});
showDialog(portraitDialog, view[0], Gravity.CENTER, true);
portraitDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
setAlpha(context, 1f);
}
});
}
/**
* 横屏打赏
*/
public static void showLandscapePlaytour(final Activity context, final String videoId, String title, final String url, String yue, String right, final String key) {
landscapeDialog = new Dialog(context, R.style.NoTitleDialog);
//默认1元
final String[] key_choose = {"1"};
setAlpha(context, 50);
final View[] view = {LayoutInflater.from(context).inflate(R.layout.layout_landscape_dialog_dashang, null)};
final ImageView[] ima_diahead_landscape = {(ImageView) view[0].findViewById(R.id.ima_diahead_landscape)};
ImageButton imbtn_colse = (ImageButton) view[0].findViewById(R.id.imbtn_colse);
TextView tv_dianame = (TextView) view[0].findViewById(R.id.tv_dianame);
TextView text_pay_1 = (TextView) view[0].findViewById(R.id.text_pay_1);
TextView text_pay_2 = (TextView) view[0].findViewById(R.id.text_pay_2);
TextView text_pay_5 = (TextView) view[0].findViewById(R.id.text_pay_5);
TextView text_pay_10 = (TextView) view[0].findViewById(R.id.text_pay_10);
TextView text_pay_15 = (TextView) view[0].findViewById(R.id.text_pay_15);
TextView text_pay_20 = (TextView) view[0].findViewById(R.id.text_pay_20);
Button button_pay = (Button) view[0].findViewById(R.id.button_pay);
TextView tv_yue = (TextView) view[0].findViewById(R.id.tv_yue);
TextView tv_button_recharge = (TextView) view[0].findViewById(R.id.tv_button_recharge);
textViews = new TextView[]{text_pay_1, text_pay_2, text_pay_5, text_pay_10, text_pay_15, text_pay_20};
setLandscapeTextColorSize(context, 0);//默认选中第一个
Glide.with(context).load(url).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL)
.transform(new CircleTransform(context))
.placeholder(R.mipmap.find_default_theatre)
.error(R.mipmap.find_default_theatre)
.crossFade()
.into(ima_diahead_landscape[0]);
if(title!=null){
tv_dianame.setText(title);
}else{
tv_dianame.setText("正在上演推荐");
}
tv_yue.setText("金币余额:" + yue);
for (int i = 0; i < textViews.length; i++) {
final int finalI = i;
textViews[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setLandscapeTextColorSize(context, finalI);
key_choose[0] = number[finalI];
// ToastUtil.showToast("充值金额=="+number[finalI]);
}
});
}
imbtn_colse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
landscapeDialog.dismiss();
}
});
mUserService = new UserService(context);
//打赏
button_pay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RestAdapterUtils.getRestAPI(CmsAPI.class).getAward(new AwardRequest(mUserService.getUserInfo().getCn_uid(), mUserService.getUserInfo().getNickname(), key_choose[0], "4", videoId, mUserService.getUserInfo().getAvatar()), new Callback<AwardBean>() {
@Override
public void success(AwardBean awardBean, Response response) {
if ("0".equals(awardBean.getCode())) {
ToastUtil.showToast("您已打赏成功,感谢您的支持!");
landscapeDialog.dismiss();
updataUserGold();
EventBus.getDefault().post(new EventType(EventType.EVENT_REFRESH));
} else if ("5".equals(awardBean.getCode())) {
ToastUtil.showToast("金币不足 请充值.");
WalletActivity.start(context);
landscapeDialog.dismiss();
} else {
ToastUtil.showToast("打赏失败");
}
}
@Override
public void failure(RetrofitError error) {
if (NetworkUtils.isNetworkAvaliable(context)) {
ToastUtil.showToast("打赏失败");
} else {
ToastUtil.showToast( "网络跑路了,请连接网络");
}
}
});
//ToastUtil.showToast("充值金额=="+key_choose[0]);
}
});
//充值
tv_button_recharge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//EventBus.getDefault().post("button_pay");
//跳转充值页面
if (NetworkUtils.isNetworkAvaliable(context)) {
WalletActivity.start(context);
landscapeDialog.dismiss();
} else {
ToastUtil.showToast( "网络跑路了,请连接网络");
}
}
});
showDialog(landscapeDialog, view[0], Gravity.CENTER, true);
landscapeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
setAlpha(context, 1f);
}
});
}
private static String gold;
private static void updataUserGold() {
RestAdapterUtils.getRestAPI(UserAPI.class).getUsersInfo(new UpdateGoldRequestBean(mUserService.getUserInfo().getCn_uid()), new Callback<UpdateGoldBalanceBean>() {
@Override
public void success(UpdateGoldBalanceBean giftInfo, Response response) {
if (giftInfo.getCode() == 0) {
gold = giftInfo.getData().getGold();
mUserService.updateGlod(gold);
}
}
@Override
public void failure(RetrofitError error) {
}
});
}
/**
* 设置文字大小颜色
*
* @param key
*/
private static void setTextColorSize(Context context, int key) {
for (int i = 0; i < textViews.length; i++) {
if (i == key) {//默认30
textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "金币", R.style.textstyle_15_ff633, R.style.textstyle_11_ff633, 2));
} else {
textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "金币", R.style.textstyle_15_999, R.style.textstyle_11_999, 2));
}
}
for (int i = 0; i < textViews.length; i++) {//设置点击背景颜色
if (i == key) {
textViews[i].setBackgroundResource(res_back[1]);
} else {
textViews[i].setBackgroundResource(res_back[0]);
}
}
}
/**
* 设置横屏文字大小颜色
*
* @param key
*/
private static void setLandscapeTextColorSize(Context context, int key) {
for (int i = 0; i < textViews.length; i++) {
if (i == key) {//默认30
textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "元/金币", R.style.textstyle_14_fff, R.style.textstyle_14_fff, 2));
} else {
textViews[i].setText(PublicUtil.formatTextView(context, number[i] + "", "元/金币", R.style.textstyle_14_fff, R.style.textstyle_14_fff, 2));
}
}
for (int i = 0; i < textViews.length; i++) {//设置点击背景颜色
if (i == key) {
textViews[i].setBackgroundResource(res_landscape_back[1]);
} else {
textViews[i].setBackgroundResource(res_landscape_back[0]);
}
}
}
/**
* 打赏
*
* @param context
* @param url
*/
public static void showShareView(final Activity context, String url) {
final Dialog dialog = new Dialog(context, R.style.NoTitleDialog);
setAlpha(context, 50);
final View[] view = {LayoutInflater.from(context).inflate(R.layout.layout_dialog_dashang, null)};
showDialog(dialog, view[0], Gravity.CENTER, true);
}
}