6 使用ProgressBar实现进度条
6-1 ProgressBar简介
ProcessBar是进度条组件,通常用户向用户展示某个耗时操作完成的进度,而不是让用户感觉是程序失去了相应,从而更好地提升用户界面的友好性。
6-2 设置ProgressBar显示风格
style="?android:attr/progressBarStyleLarge"——大环型进度条
style="?android:attr/progressBarStyleSmall"——小环型进度条
style="?android:attr/progressBarStyleHorizontal"——水平进度条
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" />
/*tyle属性:缺省情况为中环形进度条*/
<ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" /><ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar4" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
--------------------------------------------------------------------------------------------------------------------------------
6-3 标题栏中的ProgressBar
MainActivity.java
package com.example.android2_progressbar; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.Window; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 启用窗口特征,启用带进度和不带进度的进度条 requestWindowFeature(Window.FEATURE_PROGRESS);//带进度的进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//不带进度的进度条
setContentView(R.layout.activity_main);
// 显示两种进度条(显示在标题栏中的)
setProgressBarVisibility(true);//true为显示带进度的进度条
setProgressBarIndeterminateVisibility(true);//true为显示不带进度的进度条,一直转呀转
// 显示刻度 setProgress(600);//这里设置的为带进度的进度条 进度位置,max为10000也就是100%
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar4" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
6-4 关键属性和方法
关键属性:
android:max="100"最大显示进度
android:progress="50"第一显示进度(正在运行进度)
android:secondaryProgress="80"第二显示进度(缓冲进度)
android:indeterminate="true"设置是否不精确显示,true为不精确显示,false为精确显示
关键方法
setProgress(int)设置第一进度
setSecondaryProgress(int)设置第二进度
getProgress()获取第一进度
getSecondaryProgress()获取第二进度
incrementProgressBy(int)增加或者减少第一进度
incrementSecondaryProgressBy(int)增加或者减少第二进度
getMax()获取最大进度
6-5 使用ProgressBar实现进度条
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/horiz" android:max="100" android:progress="50" android:secondaryProgress="80" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add" /> <Button android:id="@+id/reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reduce" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reset" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
package com.example.android2_progressbar; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private ProgressBar progressBar; private Button addButton; private Button reduceButton; private Button resetButton; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 启用窗口特征,启用带进度和不带进度的进度条 requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); // 显示两种进度条 // 显示带进度的进度条: setProgressBarVisibility(true); // 显示不带进度的进度条 setProgressBarIndeterminateVisibility(true); // 显示刻度,设置进度条的刻度,最大值为10000 setProgress(600); init(); } private void init() { // TODO Auto-generated method stub progressBar = (ProgressBar) findViewById(R.id.horiz); addButton = (Button) findViewById(R.id.add); reduceButton = (Button) findViewById(R.id.reduce); resetButton = (Button) findViewById(R.id.reset); textView = (TextView) findViewById(R.id.text); // getProgress() // 获取第一进度条的进度 int first = progressBar.getProgress(); // 获取第二进度条的进度 int second = progressBar.getSecondaryProgress(); // 获取最大进度 int max = progressBar.getMax(); // 以百分比的形式展示 textView.setText("第一进度的百分比:" + (int) (first / (float) max * 100)[first/(float)max两个整形相除,除数强转成float类型这样两数相除得出的结果也是float.] + "% 第二进度的百分比:" + (int) (second / (float) max * 100) + "%"); addButton.setOnClickListener(this); reduceButton.setOnClickListener(this); resetButton.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.add: { // 增加第一进度和第二进度10个刻度 progressBar.incrementProgressBy(10); progressBar.incrementSecondaryProgressBy(10); break; } case R.id.reduce: { // 减少第一进度和第二进度10个刻度 progressBar.incrementProgressBy(-10); progressBar.incrementSecondaryProgressBy(-10); break; } case R.id.reset: { // 重置,为原来的进度条初值 progressBar.setProgress(50); progressBar.setSecondaryProgress(80); break; } } textView.setText("第一进度的百分比:" + (int) (progressBar.getProgress() / (float) progressBar.getMax() * 100) + "% 第二进度的百分比:" + (int) (progressBar.getSecondaryProgress() / (float) progressBar.getMax() * 100) + "%"); } }
6-6 对话框形式的进度条
package com.example.android2_progressbar; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private ProgressBar progressBar; private Button addButton; private Button reduceButton; private Button resetButton; private TextView textView; private ProgressDialog dialog; private Button showButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 启用窗口特征,启用带进度和不带进度的进度条 requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); // 显示两种进度条 // 显示带进度的进度条: setProgressBarVisibility(true); // 显示不带进度的进度条 setProgressBarIndeterminateVisibility(true); // 显示刻度,设置进度条的刻度,最大值为10000 setProgress(600); init(); } private void init() { // TODO Auto-generated method stub progressBar = (ProgressBar) findViewById(R.id.horiz); addButton = (Button) findViewById(R.id.add); reduceButton = (Button) findViewById(R.id.reduce); resetButton = (Button) findViewById(R.id.reset); showButton = (Button) findViewById(R.id.Show); textView = (TextView) findViewById(R.id.text); // getProgress() // 获取第一进度条的进度 int first = progressBar.getProgress(); // 获取第二进度条的进度 int second = progressBar.getSecondaryProgress(); // 获取最大进度 int max = progressBar.getMax(); // 以百分比的形式展示 textView.setText("第一进度的百分比:" + (int) (first / (float) max * 100) + "% 第二进度的百分比:" + (int) (second / (float) max * 100) + "%"); addButton.setOnClickListener(this); reduceButton.setOnClickListener(this); resetButton.setOnClickListener(this); showButton.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.add: { // 增加第一进度和第二进度10个刻度 progressBar.incrementProgressBy(10); progressBar.incrementSecondaryProgressBy(10); break; } case R.id.reduce: { // 减少第一进度和第二进度10个刻度 progressBar.incrementProgressBy(-10); progressBar.incrementSecondaryProgressBy(-10); break; } case R.id.reset: { // 重置,为原来的进度条初值 progressBar.setProgress(50); progressBar.setSecondaryProgress(80); break; } case R.id.Show: { /* * 页面显示风格 */ // 新建ProgressDialog的对象 dialog = new ProgressDialog(MainActivity.this); // 设置显示风格 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 横向显示 // 设置对话框标题 dialog.setTitle("木客气"); // 设置对话框的内容 dialog.setMessage("欢迎大家哈哈哈哈哈哈哈"); // 设置图标 dialog.setIcon(R.drawable.ic_launcher); /* * 设定关于progressBar进度条的一些属性 */ // 设置最大进度 dialog.setMax(100); // 设置初始化已经增长到的进度 dialog.incrementProgressBy(50); // 进度条是明确显示进度的 dialog.setIndeterminate(false); /* * 设定一个按钮 */ dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "我是大人", Toast.LENGTH_LONG).show(); } }); // 是否可以通过返回按钮退出对话框 dialog.setCancelable(true); // 显示ProgressDialog dialog.show(); break; } } textView.setText("第一进度的百分比:" + (int) (progressBar.getProgress() / (float) progressBar.getMax() * 100) + "% 第二进度的百分比:" + (int) (progressBar.getSecondaryProgress() / (float) progressBar.getMax() * 100) + "%"); } }
设置的prodialog.setCancelable(true);
是可以让你在除了确定键可以退出dialog之外,还可以通过你手机上物理的返回键或者点击对话框之外的地方退出dialog。
确定键是无论怎样都可以退出dialog的,而且设置的那个东西不代表着出现返回键的意思,只是代表一种可以被返回的功能。
6-7 自定义进度条样式
模仿系统去定义自己的样式。ctrl+左击可查看系统自带的代码
如图:
|
▼
|
▼
系统的是:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ff9d9e9d" android:centerColor="#ff5a5d5a" android:centerY="0.75" android:endColor="#ff747674" android:angle="270" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#80ffd300" android:centerColor="#80ffb600" android:centerY="0.75" android:endColor="#a0ffcb00" android:angle="270" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" /> </shape> </clip> </item> </layer-list>
我们可以根据颜色的改变去改变风格:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#123456" android:centerColor="#555555" android:centerY="0.75" android:endColor="#777777" android:angle="270" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#999999" android:centerColor="#ffffff" android:centerY="0.75" android:endColor="#676767" android:angle="270" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#eeeeee" android:centerColor="#12eddd" android:centerY="0.75" android:endColor="#989898" android:angle="270" /> </shape> </clip> </item> </layer-list>