Android-做个性化的进度条


1.案例效果图

progress[1]

2.准备素材

   image

          progress1                               progress2

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

3.原理

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

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

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

 <ProgressBar
    android: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文件,内容如下

<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

image

progress_vertical.xml文件代码如下

<?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.应用自定义的样式 

<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.点击按钮模拟进度滚动的效果

<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>
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值