zoukankan      html  css  js  c++  java
  • Android(java)学习笔记72:ProgressBar的使用

    1. ProgressBar使用

    首先我们看例程如下:

    (1) main.xml文件如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     >
     7          <TextView
     8               android:layout_width="wrap_content" 
     9            android:layout_height="wrap_content" 
    10            android:text="默认进度条:"
    11            />
    12       <ProgressBar  
    13            android:layout_width="wrap_content" 
    14            android:layout_height="wrap_content" 
    15            android:text="progress1" 
    16 //没有设置style,默认样式,中度旋转的圆形进度条 17 /> 18 <TextView 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="小圆形进度条:" 22 /> 23 <ProgressBar 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="progress3" 27 style="?android:attr/progressBarStyleSmall" 28 /> 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:text="大圆形进度条:" 33 android:layout_gravity="center_vertical" 34 /> 35 <ProgressBar 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="progress3" 39 style="?android:attr/progressBarStyleLarge" 40 /> 41 <TextView 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:text="条形进度条:" 45 android:id="@+id/tv" 46 /> 47 <ProgressBar 48 android:layout_width="fill_parent" 49 android:layout_height="wrap_content" 50 android:text="progress2" 51 android:id="@+id/porb" 52 style="?android:attr/progressBarStyleHorizontal" 53 android:max="100" 54 android:progress="50" 55 android:secondaryProgress="70" 56 /> 57 </LinearLayout>

    (2)接下来是MainActivity.java文件:

     1 package com.progressbar;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.widget.ProgressBar;
     6 //使用Runnable接口
     7 public class MainActivity extends Activity implements Runnable {
     8     private Thread th ;            //声明一条线程
     9     private ProgressBar pb ;     //声明一个进度条对象
    10     private boolean stateChage; //标识进度值最大最小的状态
    11     @Override
    12     public void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.main); 
    15         //实例进度条对象
    16         pb = (ProgressBar)findViewById(R.id.porb); 
    17         th = new Thread(this);//实例线程对象,当前MainActivity实现了Runnable接口,这样MainActivity本身可以作为参数实例化Thread()对象
    18         th.start();//启动线程
    19     }
    20 
    21     @Override
    22     public void run() {//实现Runnable接口抽象函数
    23         while(true){ 
    24             int current = pb.getProgress();//得到当前进度值
    25             int currentMax = pb.getMax();//得到进度条的最大进度值
    26             int secCurrent = pb.getSecondaryProgress();//得到底层当前进度值
    27             //以下代码实现进度值越来越大,越来越小的一个动态效果
    28             if(stateChage==false){ 
    29                 if(current>=currentMax){ 
    30                     stateChage=true; 
    31                 }else{
    32                     //设置进度值
    33                     pb.setProgress(current+1);
    34                     //设置底层进度值
    35                     pb.setSecondaryProgress(current+1);
    36                 }
    37             }else{
    38                 if(current<=0){ 
    39                     stateChage=false; 
    40                 }else{
    41                     pb.setProgress(current-1);
    42                     pb.setSecondaryProgress(current-1);
    43                 }
    44             }
    45 // 设置长方形进度条进度变大和变小的速率快慢
    46             try {
    47                 Thread.sleep(50);
    48             } catch (InterruptedException e) {
    49                 // TODO Auto-generated catch block
    50                 e.printStackTrace();
    51             }
    52         }
    53     }
    54 }

    3. 总结:

    (1)Android的ProgressBar样式

    style="?android:attr/progressBarStyleHorizontal"  长形进度条

    style="?android:attr/progressBarStyleLarge" 超大号圆形ProgressBar
    style="?android:attr/progressBarStyleLargeInverse"  超大号圆形ProgressBar,Inverse反向转向的
    style="?android:attr/progressBarStyleSmall" 小号圆形ProgressBar

    style="?android:attr/progressBarStyleSmallTitle" 标题型圆形ProgressBar

    等等还有很多

    (2)setProgress和setSecondaryProgress区别:
    这么说你肯定明白:网络视频加载的时候有个播放进度和缓冲进度,播放进度一般可以用setProgerss来显示,而setSecondaryProgress就是用来显示缓冲进度的
    同样的360的雷神引擎也是一样,setProgress是文件写入本地进度,setSecondaryProgress就是文件加载进度。还不明就自己写一个Demo测试一遍。写一个把复制文件的程序,setProgress表示往流里面读数据,setSecondaryProgerss表示从流里面输出数据
    
    
  • 相关阅读:
    Java类加载器回顾
    2018第24周总结
    JUC类图
    CopyOnWrite 策略
    Python导入模块的几种姿势
    查看linux接口进出口流量的命令;linux 网络监控;流量监控
    jenkins修改日志级别方法
    top命令查看线程信息和jstack使用介绍
    How to force immediate stop of threads in Jmeter servers如何在jmeter执行完,立即停止jmeter
    pycharm支持react
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4679347.html
Copyright © 2011-2022 走看看