Android实现进度条(ProgressBar)的功能与用法

所属分类: 软件编程 / Android 阅读数: 100
收藏 0 赞 0 分享

进度条(ProgressBar)的功能与用法,供大家参考,具体内容如下

进度条是UI界面中一种实用的UI组件,用于显示一个耗时操作显示出来的百分比,进度条可以动态的显示进度,避免是用户觉得系统长时间未反应,提高用户的体验。
下面程序简单示范了进度条的用法,界面布局文件如下:

在layout下的activity_main中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
  android:layout_height="match_parent"
  tools:context=".Main5Activity">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>
<!--  定义大环形进度条-->
  <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"/>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="任务完成进度条"
    android:textSize="24dp"/>
<!--  定义水平进度条-->
  <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/c4"
    style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>

在drawable下的文件下的my_bar中:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <!--    定义轨道的背景-->
  <item android:id="@android:id/background"
    android:drawable="@drawable/c4"/>
<!--  定义已完成部分的样式-->
  <item android:id="@android:id/progress"
    android:drawable="@drawable/c2"/>
</layer-list>

在MainActivity.java中:

package com.example.test03;

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.lang.ref.WeakReference;

public class Main5Activity extends AppCompatActivity {
//  该模拟填充长度为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<Main5Activity> activity;
    MyHandler(WeakReference<Main5Activity> activity){
      this.activity=activity;
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
//      表明该消息是该程序发送的
      if (msg.what==0x111){
        activity.get().bar.setProgress(activity.get().status);
        activity.get().bar2.setProgress(activity.get().status);
      }
    }
  }
//  负责更新进度
  MyHandler myHandler=new MyHandler(new WeakReference<>(this));
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    bar=findViewById(R.id.bar);
    bar2=findViewById(R.id.bar2);
//    启动线程在执行进度
    new Thread(){
      @Override
      public void run() {
        while (status<100){
//          获取耗时操作的完成百分比
          status=doWork();
//          发送消息
          myHandler.sendEmptyMessage(0x111);
        }
      }
    }.start();
  }
//  模拟耗时操作
  public int doWork() {
//    为数组元素赋值
    data[hasdata++] = (int) (Math.random() * 100);
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return hasdata;
  }
}

**以上就介绍到这里,上面简单实现了一些进度条的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android框架学习之Volley和Glide详解

这篇文章主要给大家介绍了关于Android框架学习之Volley和Glide的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android中Fragment的基本用法示例总结

Fragment是activity的界面中的一部分或一种行为,下面这篇文章主要给大家介绍了关于Android中Fragment的基本用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Android.mk引入第三方jar包和so库文件的方法

这篇文章主要介绍了Android.mk引入第三方jar包和so库文件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿微信录制小视频

这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

android实现一键锁屏和一键卸载的方法实例

这篇文章主要给大家介绍了关于android如何实现一键锁屏和一键卸载的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

Android手势密码--设置和校验功能的实现代码

这篇文章主要介绍了Android手势密码--设置和校验功能的实现代码,非常不错,具有一定的参考校验价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Kotlin学习笔记之const val与val

这篇文章主要给大家介绍了关于Kotlin学习笔记之const val与val的相关资料,并给大家介绍了const val和val区别以及Kotlin中var和val的区别,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android实现调用系统分享功能示例的总结

这篇文章主要介绍了通过Android调用系统分享文本信息、单张图片、多个文件和指定分享到微信、QQ,同时分享图片和文字的功能示例,小编觉得挺不错,一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android自定义view实现输入控件

这篇文章主要为大家详细介绍了Android自定义view实现输入控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码

这篇文章主要介绍了Android使用Intent.ACTION_SEND分享图片和文字内容的示例代码的实例代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多