软件编程
位置:首页>> 软件编程>> Android编程>> Android Studio实现进度条效果

Android Studio实现进度条效果

作者:Moyoshikine  发布时间:2021-10-12 05:37:17 

标签:Android,Studio,进度条

本文实例为大家分享了Android Studio实现进度条效果的具体代码,供大家参考,具体内容如下

实验作业 要求一个进度条,进度随机

效果图

Android Studio实现进度条效果

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;
//                            }
                           //获取一个随机数给到currentProgress然后显示出来
                           currentProgress = random.nextInt(100);
                           mHandler.sendEmptyMessage(0);
                       }
                   }
                   catch (InterruptedException e){
                       e.printStackTrace();
                   }
               }
           }
       }.start();
   }
}

线程里的for循环可以去掉,循环是测试定时加长进度条设计的。

来源:https://blog.csdn.net/qq_45274476/article/details/116231083

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com