//1. Activity代码,布局简单就一个按钮:
public class MainUpdateActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButton;
private SeleDialog selectDialog;
private TextView serviceTitle;
private TextView textView;
private ProgressBar progressBar;
private View serviceTitleLine;
private LinearLayout linearLayout;
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_update);
initView();
}
private void initView() {
mButton = (Button) findViewById(R.id.mButton);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mButton:
selectDialog = new SeleDialog(this, "发现新版本");
selectDialog.setCancelable(false);
selectDialog.show();
serviceTitle = selectDialog.getTitleTextView();
textView = selectDialog.getTextView();
progressBar = selectDialog.getmDialogProgressbar();
serviceTitleLine = selectDialog.getServiceTitleLine();
linearLayout = selectDialog.getmServiceDialog();
selectDialog.addListen(new SeleDialog.SelectDialogListen() {
@Override
public void onClickOk(Object obj) {
handler.sendEmptyMessage(1);
}
@Override
public void onClickCancel(Object obj) {
}
});
break;
}
}
Handler handler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
i = 0;
serviceTitle.setText("下载中");
serviceTitleLine.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
linearLayout.setVisibility(View.GONE);
new CountDownTimer(10000, 100) {
@Override
public void onTick(long millisUntilFinished) {
i += 1;
progressBar.setProgress(i);
textView.setText("请稍后... " + i + "%");
}
@Override
public void onFinish() {
selectDialog.dismiss();
Toast.makeText(MainUpdateActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
}
}.start();
}
}
};
}
//2. 自定义的Dialog弹出框 SeleDialog:
public class SeleDialog extends Dialog implements View.OnClickListener {
private View ok;
private View cancel;
private String content;
private SelectDialogListen listen;
private TextView textView;
private TextView serviceTitle;
private View serviceTitleLine;
private ProgressBar mDialogProgressbar;
private LinearLayout mServiceDialog;
public void addListen(SelectDialogListen listen) {
this.listen = listen;
}
public SeleDialog(Context context, String content) {
super(context, R.style.dialog_offer);
this.content = content;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sele_dialog);
cancel = findViewById(R.id.dialog_cancel);
serviceTitle = (TextView) findViewById(R.id.service_title);
serviceTitleLine = findViewById(R.id.service_title_line);
mDialogProgressbar = (ProgressBar) findViewById(R.id.mDialogProgressbar);
mServiceDialog = (LinearLayout) findViewById(R.id.mServiceDialog);
textView = (TextView) findViewById(R.id.dialog_content);
if (!TextUtils.isEmpty(content)) {
textView.setText(content);
}
ok = findViewById(R.id.dialog_ok);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
}
public TextView getTextView() {
return textView;
}
public TextView getTitleTextView() {
return serviceTitle;
}
public View getOkView() {
return ok;
}
public View getCancelView() {
return cancel;
}
public View getServiceTitleLine() {
return serviceTitleLine;
}
public ProgressBar getmDialogProgressbar() {
return mDialogProgressbar;
}
public LinearLayout getmServiceDialog() {
return mServiceDialog;
}
@Override
public void onClick(View v) {
//dismiss();
if (null == listen) {
return;
}
if (v == ok) {
listen.onClickOk(null);
} else if (v == cancel) {
dismiss();
listen.onClickCancel(null);
}
}
public interface SelectDialogListen {
void onClickOk(Object obj);
void onClickCancel(Object obj);
}
}
//3. Dialog的布局sele_dialog.xml:
//4.自定义ProgressBar,创建UpdateProgress:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;
public class UpdateProgress extends ProgressBar {
String text;
Paint mPaint;
public UpdateProgress(Context context) {
super(context);
// TODO Auto-generated constructor stub
initText();
}
public UpdateProgress(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initText();
}
public UpdateProgress(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initText();
}
@Override
public synchronized void setProgress(int progress) {
// TODO Auto-generated method stub
setText(progress);
super.setProgress(progress);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//this.setText();
Rect rect = new Rect();
mPaint.setTextSize(30);
this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
// int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
//进度
float radio = getProgress() * 1.0f / getMax();
float x = (int) (getWidth() * radio);
canvas.drawText(this.text, x - 65, y, this.mPaint);
}
//初始化,画笔
private void initText() {
this.mPaint = new Paint();
this.mPaint.setColor(Color.WHITE);
}
private void setText() {
setText(this.getProgress());
}
//设置文字内容
private void setText(int progress) {
int i = (progress * 100) / this.getMax();
this.text = String.valueOf(i) + "%";
}
}
//5. Dialog的style,dialog_offer:
//6. 设置自定义ProgressBar的背景,在drawable下progressbar_color.xml:
-
-
//----------------------------------------------------------------------------完--------------------------------------------------------------------------------