瘋狂 Android 講義, 4/e – 進度顯示元件(ProgressBar)範例 P130~P133
瘋狂 Android 講義, 4/e – 進度顯示元件(ProgressBar)範例 P130~P133
資料來源:
https://github.com/daichangya/book/tree/master/android
https://pan.baidu.com/s/1d_xYJI0UQ_1tQzSj_V_NIg 提取码:70ch
XML Code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 定义一个大环形进度条 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large"/> <!-- 定义一个中等大小的环形进度条 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!-- 定义一个小环形进度条 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="任务完成的进度"/> <!-- 定义一个水平进度条 --> <ProgressBar android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" style="@android:style/Widget.ProgressBar.Horizontal"/> <!-- 定义一个水平进度条,并改变轨道外观 --> <ProgressBar android:id="@+id/bar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/my_bar" style="@android:style/Widget.ProgressBar.Horizontal"/> </LinearLayout>
JAVA Code
package org.crazyit.ui; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ProgressBar; import java.lang.ref.WeakReference; /** * Description:<br> * 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a><br> * Copyright (C), 2001-2020, Yeeku.H.Lee<br> * This program is protected by copyright laws.<br> * Program Name:<br> * Date:<br> * * @author Yeeku.H.Lee kongyeeku@163.com<br> * @version 1.0 */ public class MainActivity extends Activity { // 该程序模拟填充长度为100的数组 private int[] data = new int[100]; private int hasData = 0; // 记录ProgressBar的完成进度 int status = 0; private ProgressBar bar; private ProgressBar bar2; static class MyHandler extends Handler { private WeakReference<MainActivity> activity; MyHandler(WeakReference<MainActivity> activity){ this.activity = activity; } @Override public void handleMessage(Message msg) { // 表明消息是由该程序发送的 if (msg.what == 0x111) { activity.get().bar.setProgress(activity.get().status); activity.get().bar2.setProgress(activity.get().status); } } } // 创建一个负责更新的进度的Handler MyHandler mHandler = new MyHandler(new WeakReference<>(this)); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bar = findViewById(R.id.bar); bar2 = findViewById(R.id.bar2); // 启动线程来执行任务 new Thread() { @Override public void run() { while (status < 100) { // 获取耗时操作的完成百分比 status = doWork(); // 发送消息 mHandler.sendEmptyMessage(0x111); } } }.start(); } // 模拟一个耗时的操作 public int doWork() { // 为数组元素赋值 data[hasData++] = (int)(Math.random() * 100); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return hasData; } }