Android-做个性化的进度条

 1.案例效果图

progress[1]

2.准备素材

   image

          progress1                               progress2

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

3.原理

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

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

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

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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" />   
 <ProgressBarandroid:id="@+id/progressBar2"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="268dp"android:layout_height="wrap_content"android:progress="45" /> 

image

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

4.2 找到样式定义的位置

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

          image

     样式定义的内容如下         

image

重点研究:

android:progressDrawable:进度条的样式

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

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

    image

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

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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"/><gradientandroid: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"/><gradientandroid: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"/><gradientandroid: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元素的属性上做修改

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

 

5.定义样式文件progress_vertical.xml

image

progress_vertical.xml文件代码如下

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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"><clipandroid:clipOrientation="vertical"android:drawable="@drawable/progress1"android:gravity="bottom"></clip></item>
</layer-list>

 

 

6.应用自定义的样式 

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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.    
  8.     <ProgressBar  
  9.         android:id="@+id/pbPic"  
  10.         style="@android:style/Widget.ProgressBar.Horizontal"  
  11.         android:layout_width="50dp"  
  12.         android:layout_height="68dp"  
  13.         android:background="@drawable/progress2"  
  14.         android:max="100"  
  15.         android:progress="0"  
  16.         android:progressDrawable="@drawable/progress_vertical" />    <!-- 在此属性上应用 -->  
  17.    
  18.     <TextView  
  19.         android:id="@+id/txtProgress"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"/>  
<Buttonandroid:id="@+id/btStart"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="150dp"android:text="开始"/><ProgressBarandroid: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" />    <!-- 在此属性上应用 --><TextViewandroid:id="@+id/txtProgress"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

 

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

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  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.                                        }  
  31.                                   }).start();  
  32.                  }  
  33.            });  
  34.       }</span>  
  35. }  
<span style="color:#333333;">public class  ProgressActivity extends Activity {     ProgressBar pb = null;TextView txtProgress;Handler handler = new Handler();@Overridepublicvoid 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>
}

 

 

转载于:https://www.cnblogs.com/hudabing/p/4571113.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374956.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汇编小记16/3/27

最后更新2016-03-27 21:05:06 [address]与[bx] [address] 在debug中mov ax,[0] 等价于mov ax,ds:[0] [0]表示内存偏移地址 但是在masm汇编解释器中&#xff0c;mov ax,[0] 等价于mov ax,0 [0]表示常量0 [bx] mov ax,[bx] 表示 bx存放的数据为一个偏移地址&#xff0c;段…

ConcurrentLinkedHashMap v 1.0.1发布

大家好&#xff0c;我们发布了并发LinkedHashMap实现的1.0.1版本。 在最新版本中&#xff0c;已进行了一些较小的修改&#xff0c;以在多个线程遍历映射的元素时提高性能。 最新版本还引入了可插拔驱逐策略。 当然&#xff0c;您可以实现自定义逐出策略&#xff0c;也可以将它…

BOMbing The System

roy g bivFebruary 2011 [Back to index] [Comments (0)] What is a BOM? Why should we care? Great, can we do that? Okay, lets do it! Unicode in files Greets to friendly people (A-Z) What is a BOM? Its not the thing that explodes. Thats a BOMB. Heh. BO…

鸟哥的linux私房菜学习笔记 ---第7章-2

1,文件内容查阅的命令: cat ,tac nl,more, less,head,tail ,od 文件的查阅参数,显示行号如何显示行号 nl 中的所有参数都是关于如何显示行号的 这里面less的功能更多,更灵活 :空格 下一页 pageup上一页 pagedown 下一页 /string 字符串查询 ?string 反向字符串查询 man的命…

HDU - 4497 GCD and LCM

题意&#xff1a;给出三个数的gcd,lcm&#xff0c;求这三个数的全部的可能 思路 &#xff1a;设x,y,z的gcd为d&#xff0c;那么设xd*a&#xff0c;yd*b&#xff0c;zd*c。a&#xff0c;b。c肯定是互质的。那么lcmd*a*b*c,所以我们能够得到a*b*clcm/gcdans,将ans分解因数后&…

Java Lambda语法替代

关于lambda-dev邮件列表的讨论已经开始解决lambdas /函数文字的Java语言语法应该是什么样的问题。 让我们看一个稍微平凡的例子&#xff0c;然后尝试弄清楚问题。 Perl的人有一个很好的例子&#xff0c;说明以某种功能性的方式使用函数引用–他们称其为Schwartzian变换&#xf…

浅析SMC技术

今天让我们来看Win32ASM里面的高级一点的技术——SMC&#xff08;当当当当……&#xff09;&#xff01;&#xff01;&#xff01;SMC是什么意思&#xff1f;它的英文名叫“Self Modifying Code”&#xff0c;顾名思义&#xff0c;就是“代码自修改”&#xff08;&#xff1f;&…

JAVA基础--程序是顺序执行的

class Testa {public static void main(String[] args) {String aa"aaa";String bb"bbb"aa;aa"cccc";System.out.println(bb);} } 输出的是 “bbbaaa class Testa {public static void main(String[] args) {String aa"aaa";String …

Spring MVC拦截器示例

我以为是时候看看Spring的MVC拦截器机制了&#xff0c;这种机制已经存在了很多年&#xff0c;并且是一个非常有用的工具。 Spring Interceptor会按照提示进行操作&#xff1a;在传入的HTTP请求到达您的Spring MVC控制器类之前对其进行拦截&#xff0c;或者相反&#xff0c;在其…

Android 调用系统的分享[完美实现同一时候分享图片和文字]

android 系统的分享功能 private void share(String content, Uri uri){Intent shareIntent new Intent(Intent.ACTION_SEND); if(uri!null){//uri 是图片的地址shareIntent.putExtra(Intent.EXTRA_STREAM, uri);shareIntent.setType("image/*"); //当用户选择短信时…

团队行为守则—如果你们由我来领导

&#xfeff;&#xfeff;如果你是在我领导的团队里&#xff0c;有几个额外的事情我要告诉你。我深信这些行为守则是一个高效团队的润滑剂&#xff0c;我并不只是要求别人这样做&#xff0c;我自己也严格恪守。 只有三样事&#xff1a; 问&#xff1a;如果你对任务不清楚&#…

做短,但做对!

编写简洁&#xff0c;优雅&#xff0c;清晰的代码一直是开发人员的艰巨任务。 您的同事不仅会感谢您&#xff0c;而且您会惊讶地发现&#xff0c;不断期待着重构解决方案以更少的代码完成更多&#xff08;或至少相同&#xff09;的工作是多么令人兴奋。 曾经有人说好的程序员是…

math

莫比乌斯反演&#xff1a; $F(n) \sum\limits_{d|n} {f(d)} \Leftrightarrow \sum\limits_{d|n} {\mu (d)F(\frac{n}{d})} $ 其中 ${\mu (d)}$为莫比乌斯函数: 若$d$等于0 , 则${\mu (d)}$1 若$d {p_1}{p_2}{p_3}...{p_k}$ , ${p_i}$为互异质数&#xff0c;则${\mu (d)}$${( …

(笔试题)二进制1的个数相同的距离最小数

题目&#xff1a; 输入&#xff1a;整数A输出&#xff1a;整数B条件&#xff1a;A和B的二进制1的个数相同&#xff0c;且A和B之间的距离|A-B|最小。思路&#xff1a; 题目没有说明整数类型&#xff0c;这里认为是带符号的整数&#xff0c;即区分正负数。 根据题意&#xff0c;A…

Java Swing –日期选择器对话框

房子里有Swing开发人员吗&#xff1f; 对于使用Swing的用户来说&#xff0c;这是一个GUI组件&#xff0c;可能会对您的UI编码工作有所帮助。 我们的JCG合作伙伴之一提供了日期选择器小部件。 一探究竟&#xff1a; Java Swing –日期选择器对话框以选择日期 翻译自: https://…

Casperjs中fill提交表单遇到的问题

1.if you access internet with proxy please add --ignore-ssl-errorstrue --ssl-protocolany 2.casper.then* and casper.wait* 都是异步执行的 他们的调用&#xff0c;都是按堆栈中的顺序来执行&#xff1b;也就是说&#xff0c;其他同步执行的函数&#xff0c;…

Xuggler视频处理简介

注意&#xff1a;这是我们的“ Xuggler开发教程 ”系列的一部分。 随着互联网上视频的爆炸式增长&#xff0c;开发人员经常需要在其应用程序中操纵视频内容。 Xuggler是Java开发人员的免费开放源代码库&#xff0c;可用于实时解压缩&#xff0c;处理和压缩录制的视频或实时视频…

软件测试中条件覆盖,路径覆盖,语句覆盖,分支覆盖的区别

转&#xff1a;软件测试中条件覆盖&#xff0c;路径覆盖&#xff0c;语句覆盖&#xff0c;分支覆盖的区别 举个例子吧 if A and B then Action1 if C or D then Action2 语句覆盖最弱&#xff0c;只需要让程序中的语句都执行一遍即可 …

Spring_讲解

http://s&#xff0c;i&#xff0c;s&#xff0c;h&#xff0c;u&#xff0c;o&#xff0c;k.com/forum/blogPost/list/6174.html转载于:https://www.cnblogs.com/gisblogs/p/4579162.html

使用Spring AspectJ和Maven进行面向方面的编程

Spring框架附带AOP支持。 实际上&#xff0c;如Spring参考文档中所述 &#xff0c; “ Spring的关键组件之一是AOP框架。 尽管Spring IoC容器不依赖于AOP&#xff0c;这意味着您不需要使用AOP&#xff0c;但AOP是对Spring IoC的补充&#xff0c;以提供功能强大的中间件解决方案…