wpc
2018-11-27 680fbc9e73da3e11988557cf88fd935efd3e0b1e
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
package com.haicaojie.android.util;
 
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.lcjian.library.util.SystemCommon;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.haicaojie.android.R;
import com.haicaojie.android.util.view.countdown.CountdownView;
 
/**
 * Created by weikou2015 on 2017/2/28.
 */
 
public class RedPacketDialog extends Dialog {
    public RedPacketDialog(Context context) {
        super(context);
    }
 
    public RedPacketDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public static class Builder {
        private Context context;
        private int type;
        private String portrait;
        private String title;
        private String state;
        private String redPacketFrom;
        private String time;
        private String money;
        private boolean oState;
        private String positiveButtonText;
        private OnClickListener positiveButtonClickListener;
        private String negativeButtonText;
        private DialogInterface.OnClickListener negativeButtonClickListener;
 
        public Builder(Context context) {
            this.context = context;
        }
 
        public Builder setPortrait(String portrait) {
            this.portrait = portrait;
            return this;
        }
 
        public Builder setType(int type) {
            this.type = type;
            return this;
        }
 
        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
 
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
        public Builder setState(String state) {
            this.state = state;
            return this;
        }
 
        public Builder setRedPacketFrom(String from) {
            this.redPacketFrom = from;
            return this;
        }
 
        public Builder setTime(String time) {
            this.time = time;
            return this;
        }
 
        public Builder setMoney(String money) {
            this.money = money;
            return this;
        }
 
        public Builder setoState(boolean oState) {
            this.oState = oState;
            return this;
        }
 
 
        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        DisplayImageOptions options;
 
        public RedPacketDialog create() {
            this.options = new DisplayImageOptions.Builder()
                    .showImageForEmptyUri(R.drawable.ic_red_packet_hint)
                    .showImageOnFail(R.drawable.ic_red_packet_hint)
                    .showImageOnLoading(R.drawable.ic_red_packet_hint)
                    .resetViewBeforeLoading(true).cacheInMemory(true)
                    .cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY)
                    .considerExifParams(true)
                    .displayer(new FadeInBitmapDisplayer(300)).build();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final RedPacketDialog dialog = new RedPacketDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.item_red_packet_show, null);
            dialog.addContentView(layout, new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
            ImageView iv_portrait = (ImageView) layout.findViewById(R.id.iv_portrait);
            TextView tv_title = (TextView) layout.findViewById(R.id.tv_title);
            TextView tv_from = (TextView) layout.findViewById(R.id.tv_red_packet_from);
            TextView tv_time = (TextView) layout.findViewById(R.id.tv_red_packet_time_and_des);
            TextView tv_money = (TextView) layout.findViewById(R.id.tv_money);
            CountdownView mCvRedPacket = (CountdownView) layout.findViewById(R.id.cv_redPacket);
            if (type == 1) {
                tv_from.setVisibility(View.INVISIBLE);
            } else if (type == 2) {
                tv_from.setVisibility(View.INVISIBLE);
            } else if (type == 3) {
                tv_from.setVisibility(View.INVISIBLE);
            } else if (type == 4) {
                tv_from.setVisibility(View.INVISIBLE);
            } else if (type == 5) {
                tv_from.setVisibility(View.VISIBLE);
            }
//            Glide.with(context).load(portrait).placeholder(R.drawable.ic_red_packet_hint).error(R.drawable.ic_red_packet_hint).transform(new GlideRoundTransform(context)).into(iv_portrait);
            ImageLoader.getInstance().displayImage(portrait, iv_portrait, options);
            tv_title.setText(title + "");
            tv_from.setText(redPacketFrom + "");
            if (state.equalsIgnoreCase("1")) {
                tv_money.setTextColor(context.getResources().getColor(R.color.text_red_packet_received));
                long time2 = Long.parseLong(time) - System.currentTimeMillis();
                mCvRedPacket.start(Math.abs(time2));
                tv_time.setText("倒计时:");
            } else if (state.equalsIgnoreCase("3") && oState) {
                tv_money.setTextColor(context.getResources().getColor(R.color.text_red_packet_show_color));
                tv_time.setText("你成功领取了红包");
                mCvRedPacket.setVisibility(View.GONE);
            } else if (state.equalsIgnoreCase("3") && !oState) {
                tv_money.setTextColor(context.getResources().getColor(R.color.text_red_packet_received));
                tv_time.setText("你已成功领取,不可再次领取");
                mCvRedPacket.setVisibility(View.GONE);
            } else if (state.equalsIgnoreCase("4")) {
                tv_money.setTextColor(context.getResources().getColor(R.color.text_red_packet_received));
                tv_time.setText("该订单出现退货,红包失效\n非常遗憾~");
                mCvRedPacket.setVisibility(View.GONE);
            }
//            Spannable span = new SpannableString("¥ " + money);
//            span.setSpan(new RelativeSizeSpan(2.0f), 2, money.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            String str = "¥ " + money;
            Spannable span = new SpannableString(str);
            span.setSpan(new RelativeSizeSpan(1.8f), 2, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_money.setText(span);
            // set the confirm button
            if (positiveButtonText != null) {
                if (positiveButtonClickListener != null) {
                    layout.findViewById(R.id.tv_input_des)
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            if (negativeButtonText != null) {
                if (negativeButtonClickListener != null) {
                    layout.findViewById(R.id.iv_close)
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            }
            dialog.setContentView(layout);
 
            android.view.WindowManager.LayoutParams params = dialog.getWindow()
                    .getAttributes();
            params.width = (int) ((SystemCommon.getScreenWidth(context) * 3) / 4);
            params.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setAttributes(params);
            dialog.setCanceledOnTouchOutside(false);
            return dialog;
        }
    }
}