Android Studio實(shí)現(xiàn)進(jìn)度條效果
本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)進(jìn)度條效果的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)驗(yàn)作業(yè) 要求一個(gè)進(jìn)度條,進(jìn)度隨機(jī)
效果圖

xml代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProgressBarActivity">
<ProgressBar
android:id="@+id/pb_determinate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:backgroundTint="@color/purple_200"
android:progress="25"
android:max="100"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ProgressBar"
android:textSize="28sp"
android:gravity="center"
android:layout_below="@+id/pb_determinate"
/>
</RelativeLayout>
java代碼
package com.example.a18101352;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int maxProgress;
private int currentProgress = 0;
private Handler mHandler = new Handler(){
/**
* Subclasses must implement this to receive messages.
*
* @param msg
*/
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
progressBar.setProgress(currentProgress);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);
progressBar = findViewById(R.id.pb_determinate);
maxProgress = progressBar.getMax();
}
@Override
protected void onStart(){
super.onStart();
new Thread() {
private Random random;
@Override
public void run(){
while(true){
try {
for(int i = 0; i < maxProgress; ++i){
//間隔一秒
Thread.sleep(1000);
random = new Random();
// currentProgress += 10;
// if(currentProgress > maxProgress){
// break;
// }
//獲取一個(gè)隨機(jī)數(shù)給到currentProgress然后顯示出來(lái)
currentProgress = random.nextInt(100);
mHandler.sendEmptyMessage(0);
}
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
}.start();
}
}
線程里的for循環(huán)可以去掉,循環(huán)是測(cè)試定時(shí)加長(zhǎng)進(jìn)度條設(shè)計(jì)的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android二維碼開(kāi)發(fā)學(xué)習(xí)教程
這篇文章主要為大家分享了Android二維碼開(kāi)發(fā)學(xué)習(xí)教程,感興趣的小伙伴們可以參考一下2016-07-07
Android Studio的安裝及第一次啟動(dòng)時(shí)的配置問(wèn)題
這篇文章主要介紹了Android Studio的安裝及第一次啟動(dòng)時(shí)的配置,需要的朋友可以參考下2019-09-09
Android 使用XML做動(dòng)畫(huà)UI的深入解析
在Android應(yīng)用程序,使用動(dòng)畫(huà)效果,能帶給用戶更好的感覺(jué)。做動(dòng)畫(huà)可以通過(guò)XML或Android代碼。本教程中,介紹使用XML來(lái)做動(dòng)畫(huà)。在這里,介紹基本的動(dòng)畫(huà),如淡入,淡出,旋轉(zhuǎn)等,需要的朋友可以參考下2013-07-07
TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))
下面小編就為大家?guī)?lái)一篇TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android實(shí)現(xiàn)登錄郵箱的自動(dòng)補(bǔ)全功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登錄郵箱的自動(dòng)補(bǔ)全功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Android Parcleable接口的調(diào)用源碼層分析
這篇文章主要給大家介紹了關(guān)于利用Kotlin如何實(shí)現(xiàn)Android開(kāi)發(fā)中的Parcelable的相關(guān)資料,并且給大家介紹了關(guān)于Android Parcleable源碼層問(wèn)題,需要的朋友可以參考下2022-12-12
Android性能優(yōu)化之利用Rxlifecycle解決RxJava內(nèi)存泄漏詳解
RxJava作為一種響應(yīng)式編程框架,是目前編程界網(wǎng)紅,可謂是家喻戶曉,其簡(jiǎn)潔的編碼風(fēng)格、易用易讀的鏈?zhǔn)椒椒ㄕ{(diào)用、強(qiáng)大的異步支持等使得RxJava被廣泛使用。2017-01-01

