Android 使用ProgressBar实现进度条
ProgressBar简介
ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好型。
课程目标
(1)制定ProgressBar显示风格
(2)ProgressBar的分类
(3)标题上ProgressBar的设置
(4)ProgressBar的关键属性
(5)ProgressBar的关键方法
(6)ProgressBar的基础使用
(7)自定义ProgressBar样式
制定ProgressBar显示风格
style="?android:attr/progressBarStyleLarge" 大环形进度条
style="?android:attr/progressBarStyleSmall" 小环形进度条
style="?android:attr/progressBarStyleHorizontal" 水平进度条
ProgressBar的分类
(1)可以精确显示进度(可以显示进度或者百分比)
(2)不可以精确显示进度(一直转啊转,类似于一个过场动画)
可以精确显示任务的ProgressBar可以用于显示下载任务;对于不可以精确显示进度的ProgressBar的话他对应的任务的事件可能是不可控的,也就是说我可能不知道要过多少时间才能好。
标题栏上的ProgressBar
通过一个案例来显示如何在标题栏上显示ProgressBar
通过启用窗口特征,启用带进度和不带进度的进度条
启用窗口特征——启用带进度的进度条:
requestWindowFeature(Window.FEATURE_PROGRESS);
启用窗口特征——启用不带进度的进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
启动好了之后还要把这两个进度条显示出来
显示两种进度条:
setProgressBarVisibility(true)
setProgressBarIndeterminateVisibility(true);
设置有进度的进度条的刻度(刻度的范围在0~10000)
setProgress(3000);
注意刻度不能设置成10000,因为这个时候progressBar的刻度已经到了100%,这个时候他会消失,因为进度已经完成了。
(注意:
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
这两句话要放在
setContentView(R.layout.activity_main);
之前,不然程序运行会出错。
)
第一显示进度(相当于在优酷上看电影的时候当前播放的进度)
第二显示进度(相当于在优酷上看电影的时候缓冲对应的进度)
最大显示进度(相当于一部电影的时长对应的长度)
通过第一显示进度除以最大显示进度可以得到当前进度的百分比。
ProgressBar的关键属性
android:max = "100" —— 最大显示进度
android:progress = "50" —— 第一显示进度
android:secondaryProgress="80" —— 第二显示进度
android:indeterminate = "true" —— 设置是否精确显示
true表示不精确显示进度;false表示精确显示进度
ProgressBar的关键方法
(1)setProgress(int) 设置第一进度
(2)setSecondaryProgress(int) 设置第二进度
(3)getProgress() 获取第一进度
(4)getSecondaryProgress() 获取的二进度
(5)incrementProgressBy(int) 增加或减少第一进度
(6)incrementSecondaryProgressBy(int) 增加或减少第二进度
(7)getMax() 获取最大进度
ProgressBar设置一下ProgressBar的以下方法就创建好了ProgressBar。
接下来是设置ProgressDialog,它是一个弹出的对话框(类似于之前学的DatePickerDialog和TimePickerDialog)
新建ProgressDialog对象:
progressDialog = new ProgressDialog(MainActivity.this);
/* 设定页面显示风格的属性 */
设置显示风格:
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
设置标题:
prodialog.setTitle("ProgressDialog的标题");
设置对话框里的文字信息:
prodialog.setMessage("ProgressDialog里的文字信息");
设置图标:
prodialog.setIcon(R.drawable.ic_launcher)
/* 设定关于ProgressDialog的一些属性 */
设定最大进度:
prodialog.setMax(100);
设定初始化已经增长到的进度:
prodialog.incrementProgressBy(50);
进度条是明确显示进度的:
prodialog.setIndeterminate(false);
设定确定按钮:
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了ProgressDialog的确定键", Toast.LENGTH_SHORT).show();
}
});
设定是否可以取消:
progressDialog.setCancelable(true);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <ProgressBar
android:id="@+id/progressBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <ProgressBar
android:id="@+id/progressBar4"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/> <Button
android:id="@+id/button_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增加" /> <Button
android:id="@+id/button_reduce"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="减少" /> <Button
android:id="@+id/button_reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重置" /> <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> <Button
android:id="@+id/button_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示ProgressDialog" /> </LinearLayout>
activity_main.xml
package com.example.progressbar; import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity implements OnClickListener { private ProgressBar progressBar;
private Button addButton;
private Button reduceButton;
private Button resetButton;
private TextView textView;
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(false);
setProgress(3000); progressBar = (ProgressBar) findViewById(R.id.progressBar4);
progressBar.setSecondaryProgress(10);
addButton = (Button) findViewById(R.id.button_add);
reduceButton = (Button) findViewById(R.id.button_reduce);
resetButton = (Button) findViewById(R.id.button_reset);
textView = (TextView) findViewById(R.id.textView1);
showButton = (Button) findViewById(R.id.button_show); int firstProgressNum = progressBar.getProgress();
int secondProgressNum = progressBar.getSecondaryProgress();
int maxProgressNum = progressBar.getMax();
textView.setText("第一进度:" + String.format("%.0f", (double) firstProgressNum/(double) maxProgressNum * 100.) + "%\n"
+ "第二进度:" + String.format("%.0f", (double) secondProgressNum/(double) maxProgressNum * 100.) + "%"); addButton.setOnClickListener(this);
reduceButton.setOnClickListener(this);
resetButton.setOnClickListener(this);
showButton.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_add:
if (progressBar.getProgress() + 1 <= progressBar.getMax())
progressBar.incrementProgressBy(1);
if (progressBar.getSecondaryProgress() + 1 <= progressBar.getMax())
progressBar.incrementSecondaryProgressBy(1);
break;
case R.id.button_reduce:
if (progressBar.getProgress() - 1 >= 0)
progressBar.incrementProgressBy(-1);
if (progressBar.getSecondaryProgress() - 1 >= 0)
progressBar.incrementSecondaryProgressBy(-1);
break;
case R.id.button_reset:
progressBar.setProgress(0);
progressBar.setSecondaryProgress(10);
break;
case R.id.button_show:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("ProgressDialog的标题");
progressDialog.setMessage("ProgressDialog的文字信息");
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setMax(100);
progressDialog.setIndeterminate(false);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了ProgressDialog的确定键", Toast.LENGTH_SHORT).show();
}
});
progressDialog.setCancelable(true);
progressDialog.show();
}
int firstProgressNum = progressBar.getProgress();
int secondProgressNum = progressBar.getSecondaryProgress();
int maxProgressNum = progressBar.getMax();
textView.setText("第一进度:" + String.format("%.0f", (double) firstProgressNum/(double) maxProgressNum * 100.) + "%\n"
+ "第二进度:" + String.format("%.0f", (double) secondProgressNum/(double) maxProgressNum * 100.) + "%");
}
}
MainActivity.java
自定义进度条样式
其实横向显示进度条的真正的样式是:
@android:style/Widget.ProgressBar.Horizontal
这个样式里面的最重要的其实是其中的style标签的item标签的android:progressDrawable
progressDrawable属性对应一个文件——这里是@android:drawable/progress_horizontal
可以通过给ProgressBar添加一个android:progressDrawable属性来覆盖之前的属性来达到(比如:修改颜色)的效果。
效果:
Android 使用ProgressBar实现进度条的更多相关文章
- Android学习笔记(24):进度条组件ProgressBar及其子类
ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...
- Android笔记(二十三) Android中的ProgressBar(进度条)
圆形进度条和水平进度条 进度条也是UI界面一种非常实用的组件,通常用于向用户显示某个耗时操作完成的百分比,进度条可以动态的显示进度,避免长时间的执行某个耗时操作时,让用户感觉程序失去了相应,从而更好的 ...
- Android开发 PorgressBar(进度条)的使用
圆环进度条(默认)和水平进度条: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- android 对话框中的进度条 (ProgressDialog)
from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...
- jQuery Easy UI ProgressBar(进度条)组件
ProgressBar(进度条)组件,这个还是挺好玩的.我们在自己做点什么的时候常常能用到,比方上传下载文件.导入导出文档啊.加载网页等等. 应用场景非常多,使用起来还非常easy. 演示样例: &l ...
- android中SeekBar拖动进度条的使用及事件监听
下面和大家分享一下android中SeekBar拖动进度条的使用,以及事件监听.拖动进度条的事件监听需要实现SeekBar.OnSeekBarChangeListener接口,调用SeekBar的se ...
- Android学习笔记:进度条ProgressBar的使用以及与AsyncTask的配合使用
ProgressBar时android用于显示进度的组件.当执行一个比较耗时的操作(如io操作.网络操作等),为了避免界面没有变化让用户体验降低,提供一个进度条可以让用户知道程序还在运行. 一.Pro ...
- Android ProgressBar 反向进度条/进度条从右到左走
近期的项目,有个需求须要使用条状图显示比例,而且右对齐,见下图: 我想到了使用进度条,这样不就不须要在代码动态绘制条状了,省了非常多活. 那么进度条如何从右向左显示呢? 方案一: 将ProgressB ...
- Android 打造形形色色的进度条 实现可以如此简单
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43371299 ,本文出自:[张鸿洋的博客] 1.概述 最近需要用进度条,秉着不重 ...
随机推荐
- iOS--崩溃日志的格式化分析---格式化crash日志
工作中难免或碰到crash,如果是开发环境,碰到简单的crash还能重现下,如果不能重现的话,我们只能去分crash文件了. 首先看下面的crash问题,说句实话一看这个我是拒绝的,这怎么找原因啊,头 ...
- iOS runtime探究(二): 从runtime開始深入理解OC消息转发机制
你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639289 本文主要解说runtime相关知识, ...
- datagridview导出到excel
Microsoft.Office.Interop.Excel.Range range = null; string saveFileName = ""; bool fileSave ...
- 测试markdown编辑器
标题1 标题2 +++ 第一件事 +++ 第二件事 +++ 第三件事 |head|头|头栏| |body|body|body|
- mysql 存储过程 invoker invoker
方法一:修改存储过程的definer update mysql.proc set definer='root@localhost' where db='db_name'; 方法二:修改sql secu ...
- Fork of LGPL version of JPedal
https://github.com/on-site/JPedal —————————————————————————————————————————————————————————————————— ...
- 数据库 Oracle数据库性能优化
--在Oacle数据库涉及到全表扫描的SQL查询(top,count)中, --现场用户删除表中大部分数据,只保留1W条数据,但是查询仍然很慢,检查磁盘IO,发现磁盘IO不是很高 --经过分析Oacl ...
- TensorFlow基础笔记(11) conv2D函数
#链接:http://www.jianshu.com/p/a70c1d931395 import tensorflow as tf import tensorflow.contrib.slim as ...
- e681. 基本的打印程序
Note that (0, 0) of the Graphics object is at the top-left of the actual page, outside the printable ...
- PS流的格式和解析总结
对于PS流,最近因为工作需要,所以MPEG2中的PS流格式和解包过程进行了学习. 首先我们需要知道PS包流格式是怎么样的: (来自http://blog.csdn.net/chen495810242/ ...