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;
|
}
|
}
|
}
|