Android之ProgressBar

今天复习一下以前的知识,补充一下ProgressBar控件

  progressBar是进度条组件,通常用于用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性。

  1)制定ProgressBar显示风格

  2)ProgressBar的分类

  3)标题上ProgressBar的设置

  4)ProgressBar的关键属性

  5)ProgressBar的关键方法

  6)ProgressDialog的基础使用

  7)自定义ProgressBar样式

 

1)制定ProgressBar显示风格

  style = "?android:attr/progressBarStyleLarge"    大环形进度条

  style = "?android:attr/progressBarStyleSmall"   小环形进度条

  style = "?android:attr/progressBarStyleHorizontal"   水平进度条

  

 

  

 

2)ProgressBar的分类

  1.可以精确显示进度(可以显示刻度或者百分比)

  

  2.不可以精确显示精度(一直转啊转,类似于一个过场动画)

 

3)标题上ProgressBar的设置

  

  

 

 4)ProgressBar的关键属性

  android:max = "100" ——最大显示进度

  android:progress = "50" ——第一显示进度

  android:secondaryProgress = "80" ——第二显示进度

  android:indeterminate = "true" ——设置是否精确显示

    (true表示不精确显示进度,false表示精确显示进度)

5)ProgressBar的关键方法

  1)setProgress(int) 设置第一进度

  2)setSecondaryProgress(int) 设置第二进度

  3)getProgress() 获取第一进度

  4)getSecondaryProgress() 获取第二进度

  5)incrementProgressBy(int) 增加或减少第一进度

  6)incrementSecondaryProgressBy(int) 增加或减少第二进度

  7)getMax() 获取最大进度

  

案例:

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context="com.example.progressbar.MainActivity" >
 7 
 8     <ProgressBar
 9         android:id="@+id/progressBar1"
10         style="?android:attr/progressBarStyleLarge"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content" />
13 
14     <ProgressBar
15         android:id="@+id/progressBar2"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"/><!-- 没有设置默认中环形进度条 -->
18 
19     <ProgressBar
20         android:id="@+id/progressBar3"
21         style = "?android:attr/progressBarStyleSmall"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"/>
24     
25     <ProgressBar
26         android:secondaryProgress="80"
27         android:progress="50"
28         android:max="100"
29         android:id="@+id/horiz"
30         style="?android:attr/progressBarStyleHorizontal"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"/>
33 
34     <Button
35         android:id="@+id/add"
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content"
38         android:text="增加" />
39 
40     <Button
41         android:id="@+id/reduce"
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:text="减少" />
45 
46     <Button
47         android:id="@+id/reset"
48         android:layout_width="wrap_content"
49         android:layout_height="wrap_content"
50         android:text="重置" />
51 
52     <TextView
53         android:id="@+id/text"
54         android:layout_width="wrap_content"
55         android:layout_height="wrap_content"/>
56 
57 </LinearLayout>
activity_main.xml

 

 1 package com.example.progressbar;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnClickListener;
 7 import android.view.Window;
 8 import android.widget.Button;
 9 import android.widget.ProgressBar;
10 import android.widget.TextView;
11 
12 
13 public class MainActivity extends Activity implements OnClickListener {
14     
15     private ProgressBar progress ;
16     private Button add;
17     private Button reduce;
18     private Button reset;
19     private TextView text;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         //启用窗口特征,启用带进度和不带进度的进度条
24         requestWindowFeature(Window.FEATURE_PROGRESS);
25         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
26         setContentView(R.layout.activity_main);
27         //显示两种进度条
28         setProgressBarVisibility(true);//水平进度条
29         setProgressBarIndeterminateVisibility(true);//环形进度条
30         //Max = 10000
31         setProgress(9999);
32         init();
33         
34     }
35 
36 
37     private void init() {
38         progress = (ProgressBar) findViewById(R.id.horiz);
39         add = (Button) findViewById(R.id.add);
40         reduce = (Button) findViewById(R.id.reduce);
41         reset = (Button) findViewById(R.id.reset);
42         text = (TextView) findViewById(R.id.text);
43         //getPeogress()获取第一进度
44         int first = progress.getProgress();
45         //获取第二进度
46         int second = progress.getSecondaryProgress();
47         //获取最大进度
48         int max = progress.getMax();
49         
50         text.setText("第一进度百分比:"+(int)(first/(float)max*100)+"%,第一进度百分比:"+(int)(second/(float)max*100)+"%");
51         add.setOnClickListener(this);
52         reduce.setOnClickListener(this);
53         reset.setOnClickListener(this);
54     }
55 
56     @Override
57     public void onClick(View v) {
58         switch(v.getId()){
59             case R.id.add :{
60                 //增加第一进度第二进度10个刻度
61                 progress.incrementProgressBy(10);
62                 progress.incrementSecondaryProgressBy(10);
63                 
64                 break ;
65             }
66             case R.id.reduce :{
67                 progress.incrementProgressBy(-10);
68                 progress.incrementSecondaryProgressBy(-10);
69                 
70                 break ;
71             }
72             case R.id.reset :{
73                 progress.setProgress(50);
74                 progress.setSecondaryProgress(80);
75                 
76                 break ;
77             }
78         }
79         text.setText("第一进度百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"%,第一进度百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax() * 100)+"%");
80     }
81 }
MainActivity.java

 

 

6)ProgressDialog的基础使用

 

 1 private ProgressDialog prodialog ;
 2 private Button show;
 3 
 4 show = (Button) findViewById(R.id.show);
 5 show.setOnClickListener(this);
 6 
 7 
 8 case R.id.show :{
 9 /**
10 * 页面显示风格
11 */
12 //新建ProgressDialog对象
13 prodialog = new ProgressDialog(MainActivity.this);
14 //设置显示风格
15 prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
16 //设置标题
17 prodialog.setTitle("南方IT学院");
18 //设置对话框文字信息
19 prodialog.setMessage("欢迎大家");
20 //设置图标
21 prodialog.setIcon(R.drawable.ic_launcher);
22 /**
23 * 设置关于ProgressBar属性
24 */
25 //设置最大进度
26 prodialog.setMax(100);
27 //设定初始化已经增长到的进度
28 prodialog.incrementProgressBy(50);
29 //进度条是明显显示进度的
30 prodialog.setIndeterminate(false);
31 
32 /**
33 * 设定一个确定按钮
34 */
35 prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
36 
37 @Override
38 public void onClick(DialogInterface dialog, int which) {
39 Toast.makeText(MainActivity.this, "欢迎", Toast.LENGTH_LONG).show();
40 }
41 });
42 //是否可以通过返回按钮退出对话框
43 prodialog.setCancelable(true);
44 
45 //显示ProgressDialog
46 prodialog.show();
47 break ;
48 }
MainActivity

 

 

 

7)自定义ProgressBar样式

style="@android:style/Widget.ProgressBar.Horizontal"
android:progressDrawable="@drawable/progress_bar"

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <item android:id="@android:id/background">
 5         <shape>
 6             <corners android:radius="5dip" />
 7 
 8         <solid android:color="#88000000"/>
 9         </shape>
10     </item>
11     <item android:id="@android:id/secondaryProgress">
12         <clip>
13             <shape>
14                 <corners android:radius="5dip" />
15 
16                 <gradient
17                     android:angle="270"
18                     android:centerColor="#C6B7FF"
19                     android:centerY="0.75"
20                     android:endColor="#C3B2FF"
21                     android:startColor="#B9A4FF" />
22             </shape>
23         </clip>
24     </item>
25     <item android:id="@android:id/progress">
26         <clip>
27             <shape>
28                 <corners android:radius="5dip" />
29 
30             <gradient
31                 android:angle="270"
32                 android:centerColor="#74EBFF"
33                 android:centerY="0.75"
34                 android:endColor="#8EEFFF"
35                 android:startColor="#57E8FF" />
36             </shape>
37         </clip>
38     </item>
39 
40 </layer-list>
progress_bar.xmml

 

posted on 2016-12-12 14:41  根坤  阅读(5496)  评论(0编辑  收藏  举报

导航