一步一步学android控件(之二十三)

android中内置了多种风格的ProgressBar(进度条) ,通过style属性设置其样式:

默认的样式

圆形稍大一点的ProgressBar ,style="?android:attr/progressBarStyleLarge"

水平的ProgressBar ,style="?android:attr/progressBarStyleHorizontal"

我们可以将ProgressBar的样式分为两大类型:

1、圆形动画: 一般用于一些未知“长度”的任务,仅仅表示正在处理任务,但是不知道任务进行到了何处 。

2、水平进度条:一般用于要显示中间进度。条件:知道任务的长度;当前任务完成度。

本文结合两个实例来学习progressBar。

1、定位 + 自定义 圆形动画的 progressBar

点击“位置服务”时,启动一个线程去加载用户的位置信息,同时显示圆形动画的progressBar。位置信息加载“完成”后发送一个消息更新UI——显示用户位置信息或者显示定位出错信息。

2、文件读取 + 自定义样式的水平progressBar + 文件内容显示

点击“文件操作”时,启动一个线程读取文件中的信息。所有读取出来的信息,一个字符一个字符的写到TextView中(为了能够清晰的看清楚操作进度 , 每写一个字符线程休眠500毫秒:

下面一步一步实现上述功能。

1、准备一个文件test_file.txt放到assets目录下

中文测试字符。First line .second line .third line .forth line .fifth line .sixth line .seventh line .eighth line .ninth line .tenth line .2、创建布局文件widget_progressbar_layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:id="@+id/load_location_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/str_location_service" /><Buttonandroid:id="@+id/load_file_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/str_file_op" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:orientation="horizontal" ><ProgressBarandroid:id="@+id/location_progress_bar"style="@style/customer_progress_style"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="invisible" /><TextViewandroid:id="@+id/location_text_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="invisible" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="150dp"android:orientation="vertical" ><ProgressBarandroid:id="@+id/file_reader_progress"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="invisible"style="@style/customer_progress_style_horizontal" /><!– style="?android:attr/progressBarStyleHorizontal" –><TextViewandroid:id="@+id/file_content_text_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="visible" /></LinearLayout></LinearLayout>布局文件中有两个progressBar :

2.1 自定义圆形动画的progressBar : android:id="@+id/location_progress_bar" 为位置服务的progressBar 其使用了样式style="@style/customer_progress_style" ,该样式定义在res/values/styles.xml 中

<style name="customer_progress_style" ><item name="android:indeterminateDrawable">@drawable/progress_style_circle_anim</item></style>

style中使用了自定义的drawable ——progress_style_circle_anim.xml , 其内容如下:

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android=""android:duration="500"android:fromDegrees="0"android:pivotX="50%"android:pivotY="50%"android:repeatCount="-1"android:repeatMode="reverse"android:toDegrees="360" ><shapexmlns:android=""android:dither="true"android:innerRadiusRatio="4"android:shape="ring"android:thicknessRatio="20"android:useLevel="false" ><paddingandroid:bottom="5dp"android:left="5dp"android:right="5dp"android:top="5dp" /><strokeandroid:dashGap="3dp"android:dashWidth="7dp"android:color="#FFFF00" /><gradientandroid:centerColor="@color/pregress_style_center_color"android:centerX="50%"android:centerY="50%"android:endColor="@color/pregress_style_end_color"android:startColor="@color/pregress_style_start_color"android:type="sweep" /></shape></rotate>2.2 自定义水平样式的progressBar :android:id="@+id/file_reader_progress" 为显示文件操作进度的progressBar , 其使用了样式style="@style/customer_progress_style_horizontal" ,该样式定义在res/values/styles.xml 中

活在当下,别在怀念过去或者憧憬未来中浪费掉你现在的生活。

一步一步学android控件(之二十三)

相关文章:

你感兴趣的文章:

标签云: