//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:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:paddingBottom="10dp">
<LinearLayout
android:id="@+id/mReServiceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp">
<TextView
android:id="@+id/service_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="提示"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:text="发现新版本"
android:id="@+id/dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mReServiceTitle"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:minHeight="30dp"
android:textSize="16sp" />
<com.gang.app.myceshi.UpdateProgress
android:id="@+id/mDialogProgressbar"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/dialog_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:progressDrawable="@drawable/progressbar_color"
android:visibility="gone" />
<LinearLayout
android:id="@+id/service_title_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/dialog_content"
android:layout_marginTop="15dp"
android:background="#EEEEEE"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/mServiceDialog"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/service_title_line"
android:orientation="horizontal">
<TextView
android:id="@+id/dialog_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="12dp"
android:paddingTop="15dp"
android:text="取消"
android:textColor="#8F8F8F"
android:textSize="18sp" />
<View
android:id="@+id/dialog_split_view"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#EEEEEE"
android:paddingBottom="12dp"
android:paddingTop="15dp" />
<TextView
android:id="@+id/dialog_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="12dp"
android:paddingTop="15dp"
android:text="确定"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
//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:
<style name="dialog_offer" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
//6. 设置自定义ProgressBar的背景,在drawable下progressbar_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置背景色 -->
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:endColor="#cccccc"
android:startColor="#cccccc" />
</shape>
</item>
<!-- 设置进度条颜色 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:endColor="#35da7c"
android:startColor="#5bd259" />
</shape>
</clip>
</item>
</layer-list>
//—————————————————————————-完——————————————————————————–
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118292.html