1.案例效果图

2.准备素材

                              

progress1.png(78*78)              progress2.png(78*78)

3.原理

采用一张图片作为ProgressBar的背景图片(一般采用颜色比较浅的)。另一张是进度条的图片(一般采用颜色比较深的图片)。进度在滚动时:进度图片逐步显示,背景图片逐步隐藏,达到上面的效果。

4.灵感来自Android控件提供的源码

4.1 默认带进度的进度条,如下图

  1. <ProgressBar
  2. android:id="@+id/progressBar2"
  3. style="@android:style/Widget.ProgressBar.Horizontal"
  4. android:layout_width="268dp"
  5. android:layout_height="wrap_content"
  6. android:progress="45" />
 <ProgressBar
android:id="@+id/progressBar2"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="268dp"
android:layout_height="wrap_content"
android:progress="45" />

注意:关键是style属性在起作用

4.2 找到样式定义的位置

鼠标放在style属性值上,按下Ctrl键,出现超链接,点击超链接跳转到样式的定义位置

样式定义的内容如下

重点研究:

android:progressDrawable:进度条的样式

@android:drawable/progress_horizontal:样式定义的文件

在android-sdk-windows\platforms\android-14\data\res目下搜索progress_horizontal.xml文件,搜索结果如下:

打开progress_horizontal.xml文件,内容如下

  1. <layer-listxmlns:androidlayer-listxmlns:android="http://schemas.android.com/apk/res/android">
  2. <itemandroid:iditemandroid:id="@android:id/background">
  3. <shape>
  4. <cornersandroid:radiuscornersandroid:radius="5dip"/>
  5. <gradient
  6. android:startColor="#ff9d9e9d"
  7. android:centerColor="#ff5a5d5a"
  8. android:centerY="0.75"
  9. android:endColor="#ff747674"
  10. android:angle="270"
  11. />
  12. </shape>
  13. </item>
  14. <itemandroid:iditemandroid:id="@android:id/secondaryProgress">
  15. <clip>
  16. <shape>
  17. <cornersandroid:radiuscornersandroid:radius="5dip"/>
  18. <gradient
  19. android:startColor="#80ffd300"
  20. android:centerColor="#80ffb600"
  21. android:centerY="0.75"
  22. android:endColor="#a0ffcb00"
  23. android:angle="270"
  24. />
  25. </shape>
  26. </clip>
  27. </item>
  28. <itemandroid:iditemandroid:id="@android:id/progress">
  29. <clip>
  30. <shape>
  31. <cornersandroid:radiuscornersandroid:radius="5dip"/>
  32. <gradient
  33. android:startColor="#ffffd300"
  34. android:centerColor="#ffffb600"
  35. android:centerY="0.75"
  36. android:endColor="#ffffcb00"
  37. android:angle="270"
  38. />
  39. </shape>
  40. </clip>
  41. </item>
  42. </layer-list>
<layer-listxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:id="@android:id/background">
<shape>
<cornersandroid:radius="5dip"/>
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<itemandroid:id="@android:id/secondaryProgress">
<clip>
<shape>
<cornersandroid:radius="5dip"/>
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<itemandroid:id="@android:id/progress">
<clip>
<shape>
<cornersandroid:radius="5dip"/>
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>

释义:

<item android:id="@android:id/background">:定义进度条的背景样式

<item android:id="@android:id/secondaryProgress">:辅助进度条的样式

<item android:id="@android:id/progress">:进度条的样式

思考:如果我想做垂直进度条,怎么办了?

关键在clip元素的属性上做修改

<clip
android:clipOrientation="vertical" 定义滚动的方向 vertical为垂直方向
android:drawable="@drawable/progress1" 定义进度的图片
android:gravity="bottom" > 定义进度的开始位置
</clip>

5.定义样式文件progress_vertical.xml

progress_vertical.xml文件代码如下

  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?>
  2. <layer-listxmlns:androidlayer-listxmlns:android="http://schemas.android.com/apk/res/android">
  3. <itemandroid:iditemandroid:id="@android:id/progress">
  4. <clip
  5. android:clipOrientation="vertical"
  6. android:drawable="@drawable/progress1"
  7. android:gravity="bottom">
  8. </clip>
  9. </item>
  10. </layer-list>
<?xmlversion="1.0"encoding="utf-8"?>
<layer-listxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:drawable="@drawable/progress1"
android:gravity="bottom">
</clip>
</item>
</layer-list>

6.应用自定义的样式

  1. <Button
  2. android:id="@+id/btStart"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="150dp"
  6. android:text="开始"/>
  7. <ProgressBar
  8. android:id="@+id/pbPic"
  9. style="@android:style/Widget.ProgressBar.Horizontal"
  10. android:layout_width="50dp"
  11. android:layout_height="68dp"
  12. android:background="@drawable/progress2"
  13. android:max="100"
  14. android:progress="0"
  15. android:progressDrawable="@drawable/progress_vertical" />    <!-- 在此属性上应用 -->
  16. <TextView
  17. android:id="@+id/txtProgress"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"/>
<Button
android:id="@+id/btStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="开始"/> <ProgressBar
android:id="@+id/pbPic"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="50dp"
android:layout_height="68dp"
android:background="@drawable/progress2"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/progress_vertical" /> <!-- 在此属性上应用 --> <TextView
android:id="@+id/txtProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

7.点击按钮模拟进度滚动的效果

  1. <span style="color:#333333;">public class  ProgressActivity extends Activity {
  2. ProgressBar pb = null;
  3. TextView txtProgress;
  4. Handler handler = new Handler();
  5. @Override
  6. publicvoid onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. System.out.println("主题=" + getTheme() + "");
  10. pb = (ProgressBar) findViewById(R.id.pbPic);
  11. Button btnStart = (Button) findViewById(R.id.btStart);//按钮
  12. txtProgress = (TextView) findViewById(R.id.txtProgress);//显示进度
  13. btnStart.setOnClickListener(new OnClickListener() {//按钮点击事件
  14. publicvoid onClick(View v) {
  15. new Thread(new Runnable() {//创建并启动线程,使用线程执行模拟的任务
  16. publicvoid run() {
  17. for (inti = 0; i < 100; i++) { //循环100遍
  18. try {
  19. handler.post(new Runnable() { //更新界面的数据
  20. publicvoid run() {
  21. pb.incrementProgressBy(1);//增加进度
  22. txtProgress.setText(pb.getProgress() + "%");//显示完成的进度
  23. }
  24. });
  25. Thread.sleep(100);
  26. } catch (InterruptedException e) {
  27. }
  28. }
  29. }
  30. }).start();
  31. }
  32. });
  33. }</span>
  34. }
<span style="color:#333333;">public class  ProgressActivity extends Activity {
ProgressBar pb = null;
TextView txtProgress;
Handler handler = new Handler();
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("主题=" + getTheme() + "");
pb = (ProgressBar) findViewById(R.id.pbPic);
Button btnStart = (Button) findViewById(R.id.btStart);//按钮
txtProgress = (TextView) findViewById(R.id.txtProgress);//显示进度
btnStart.setOnClickListener(new OnClickListener() {//按钮点击事件
publicvoid onClick(View v) {
new Thread(new Runnable() {//创建并启动线程,使用线程执行模拟的任务
publicvoid run() {
for (inti = 0; i < 100; i++) { //循环100遍
try {
handler.post(new Runnable() { //更新界面的数据
publicvoid run() {
pb.incrementProgressBy(1);//增加进度
txtProgress.setText(pb.getProgress() + "%");//显示完成的进度
}
});
Thread.sleep(100);
} catch (InterruptedException e) { }
}
}
}).start();
}
});
}</span>
}

 

Android-做个性化的进度条的更多相关文章

  1. Android学习笔记- ProgressBar(进度条)

    本节引言: 本节给大家带来的是Android基本UI控件中的ProgressBar(进度条),ProgressBar的应用场景很多,比如 用户登录时,后台在发请求,以及等待服务器返回信息,这个时候会用 ...

  2. Android 使用ProgressBar实现进度条

    ProgressBar简介ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好型. 课程目标(1)制定Progre ...

  3. android 对话框中的进度条 (ProgressDialog)

    from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...

  4. Android学习笔记(24):进度条组件ProgressBar及其子类

    ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...

  5. android中SeekBar拖动进度条的使用及事件监听

    下面和大家分享一下android中SeekBar拖动进度条的使用,以及事件监听.拖动进度条的事件监听需要实现SeekBar.OnSeekBarChangeListener接口,调用SeekBar的se ...

  6. Android开发 PorgressBar(进度条)的使用

    圆环进度条(默认)和水平进度条: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  7. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  8. Android 自定义圆形旋转进度条,仿微博头像加载效果

    微博 App 的用户头像有一个圆形旋转进度条的加载效果,看上去效果非常不错,如图所示: 据说 Instagram 也采用了这种效果.最近抽空研究了一下,最后实现的效果是这样: 基本上能模拟出个大概,代 ...

  9. Android 打造形形色色的进度条 实现可以如此简单

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43371299 ,本文出自:[张鸿洋的博客] 1.概述 最近需要用进度条,秉着不重 ...

随机推荐

  1. Jenkins学习三:介绍一些Jenkins的常用功能

    Jenkins其实就是一个工具,这个工具的作用就是调用各种其他的工具来达成你的目的. 1.备份.迁移.恢复jenkins 首先找到JENKINS_HOME,因为Jenkins的所有的数据都是以文件的形 ...

  2. 嵌入式Linux驱动学习之路(十六)输入子系统

    以前写的一些输入设备的驱动都是采用字符设备处理的.问题由此而来,Linux开源社区的大神们看到了这大量输入设备如此分散不堪,有木有可以实现一种机制,可以对分散的.不同类别的输入设备进行统一的驱动,所以 ...

  3. 洛谷P1111 修复公路

    题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你什么时 ...

  4. StackExchange.Redis通用封装类分享(转)

    阅读目录 ConnectionMultiplexer 封装 RedisHelper 通用操作类封 String类型的封装 List类型的封装 Hash类型的封装 SortedSet 类型的封装 key ...

  5. 生成 PDF 全攻略【2】在已有PDF上添加内容

    项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...

  6. C#基础系列——多线程的常见用法详解

    前言:前面几节分别介绍了下C#基础技术中的反射.特性.泛型.序列化.扩展方法.Linq to Xml等,这篇跟着来介绍下C#的另一基础技术的使用.最近项目有点紧张,所以准备也不是特别充分.此篇就主要从 ...

  7. 新项目,WebTest

    最近为了给Jumony for ASP.NET进行单元测试有点伤神,ASP.NET因为环境特殊,一直是单元测试的禁地,传统的单元测试工具由于运行在非ASP.NET环境,可谓是举步维艰.当然,微软在搞A ...

  8. Visual Studio 2015 Pre Secondary Installer 在哪里

    安装vs2015 pre后,会自动打开Secondary Installer, 用于Cross Platform的移动开发框架,包括Cordova插件.若安装失败,启动程序位置: "D:\P ...

  9. APP

    第一天 1.环境初步搭建 SmartSniff fiddler ddms AntiDroid Android Killer 百度方法 http://jingyan.baidu.com/article/ ...

  10. 未封装的js放大镜特效

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>j ...