Android ProgressBar自定义图片进度,自定义渐变色进度条

java.lang.Object
2   ↳    android.view.View
3       ↳    android.widget.ProgressBar
4直接子类
5AbsSeekBar
6间接子类
7RatingBar, SeekBar

在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中

1、android.widget. ProgressBar,继承自android.view.View 。在android.widget包中。对应对话框ProgressDialog。ProgressBar有两种展示方式,表盘形式(普通、小、大)和条形填充形式。在layout定义时,需要通过设施style属性类设置展示方式。

ProgressBar的样式有四种:

android:progressBarStyle:默认进度条样式,不确定模式
android:progressBarStyleHorizontal:水平进度条样式
android:progressBarStyleLarge :大号进度条样式,也是不确定进度模式

android:progressBarStyleSmall :小号进度条样式,也是不确定进度模式

二、XML重要属性

             android:max--  这事进度条长度最大值

             android:progress--设定度条当前进度值

            android:secondaryProgress--第二进度条进度值

    android:progressBarStyle:默认进度条样式

    android:progressBarStyleHorizontal:水平样式

      style="?android:attr/progressBarStyleLarge" --- 属性风格类型--大圆圈,如下图

       style=”?android:attr/progressBarStyleSmall”--- 属性风格类型--小圆圈,如下图:

      

      style="?android:attr/progressBarStyleHorizontal"  --水平进度条 --如下图:

      

        几秒钟之后自动滚到到如下:

      

也可以用下面的形式代替上面的形式的:

1<ProgressBarstyle="@android:style/Widget.ProgressBar.Inverse"/>//中
2<ProgressBarstyle="@android:style/Widget.ProgressBar.Large.Inverse"/> //大圆
3<ProgressBarstyle="@android:style/Widget.ProgressBar.Small.Inverse"/> //小圆


三、重要方法

    getMax():返回这个进度条的范围的上限

    getProgress():返回当前进度值

    getSecondaryProgress():返回次要当前进度值

    incrementProgressBy(int diff):指定增加的进度--即步长

    isIndeterminate():指示进度条是否在不确定模式下

    setIndeterminate(boolean indeterminate):设置不确定模式下

    setVisibility(int v):设置该进度条是否可视

四、重要事件

    onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件

接下来看案例:

1.定义一个布局文件progressbar.xml

01<?xmlversion="1.0"encoding="utf-8"?>
02<ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"
03
04    android:layout_width="fill_parent"
05    android:layout_height="match_parent"
06    >
07    <LinearLayout
08        android:layout_width="fill_parent"
09        android:layout_height="match_parent"
10        android:orientation="vertical">
11<TextView
12    android:id="@+id/startText"
13    android:layout_width="fill_parent"
14    android:layout_height="wrap_content"
15    android:text="垂直的----标题上面也有一个进度条哦"
16    android:textColor="#CD0000"
17    android:background="#BC8F8F"
18    />
19   <!-- style=”?android:attr/progressBarStyleLarge”大圆圈 -->
20    <ProgressBar
21      android:id="@+id/progtessBer_btn_id1"
22      android:layout_width="wrap_content"
23      android:layout_height="wrap_content"
24      style="?android:attr/progressBarStyleLarge"
25        />
26     <!-- style=”?android:attr/progressBarStyleSmall”小圆圈 -->
27    <ProgressBar
28      android:layout_width="wrap_content"
29      android:layout_height="wrap_content"
30      style="?android:attr/progressBarStyleSmall"
31      android:layout_gravity="center_horizontal"
32        />
33    <TextView
34    android:id="@+id/startText1"
35    android:layout_width="fill_parent"
36    android:layout_height="wrap_content"
37    android:text="水平的"
38    android:textColor="#aaaaaa"
39    />
40     <!--  style="?android:attr/progressBarStyleHorizontal"  水平进度条 -->
41    <ProgressBar
42     android:id="@+id/progtessBer_btn_id2"
43     android:layout_width="fill_parent"
44     android:layout_height="wrap_content"
45     style="?android:attr/progressBarStyleHorizontal"
46     />
47    <TextView
48     android:layout_width="fill_parent"
49     android:layout_height="wrap_content"
50     android:text="@string/progress_text"
51     />
52    </LinearLayout>
53</ScrollView>

2.之后定义java文件:ProgressBarDemo.java

01package com.dream.app.start.first.prograssbar;
02import com.dream.app.start.MenuDemo;
03import com.dream.app.start.R;
04import com.dream.app.start.R.id;
05import com.dream.app.start.R.layout;
06import com.dream.app.start.utils.PublicClass;
07 
08import android.app.Activity;
09import android.content.Intent;
10import android.os.Bundle;
11import android.os.Handler;
12import android.text.method.ScrollingMovementMethod;
13import android.view.View;
14import android.view.View.OnClickListener;
15import android.view.Window;
16import android.widget.AdapterView;
17import android.widget.AdapterView.OnItemClickListener;
18import android.widget.ArrayAdapter;
19import android.widget.Button;
20import android.widget.ListView;
21import android.widget.ProgressBar;
22import android.widget.TextView;
23import android.widget.Toast;
24 
25public class ProgressBarDemo extendsPublicClass {
26    privateProgressBar  progressbar,progressbar_1;
27    Button  btn1,btn2;
28    privateint prostatus=0;
29    //创建一个handler对象
30    private Handler  handler=newHandler();
31    @Override
32    protectedvoidonCreate(Bundle savedInstanceState) {
33        // TODO Auto-generated method stub
34        super.onCreate(savedInstanceState);
35       
36       
37          //在标题条里放置进度条。请求窗口特色风格,这里设置成不明确的进度风格
38          requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
39        
40          //设置窗口进度条特性风格
41//         requestWindowFeature(Window.FEATURE_PROGRESS);
42 
43        
44          setContentView(R.layout.progressbar);
45          //设置标题栏中的不明确的进度条是否可以显示,当你需要表示处理中的时候设置为True,处理完毕后设置为false
46 
47          setProgressBarIndeterminateVisibility(true);
48       
49        //设置进度条进度值,要乘以100的
50//          setProgress(60*100);
51//          setSecondaryProgress(80*100);
52 
53        btn2=(Button)findViewById(R.id.button_cancel);
54//      btn2.setOnClickListener(onClick);
55        progressbar=(ProgressBar)findViewById(R.id.progtessBer_btn_id2);
56        progressbar_1=(ProgressBar)findViewById(R.id.progtessBer_btn_id1);
57        //设置进度条的最大值
58        progressbar.setMax(100000);
59        progressbar_1.setMax(100000);
60        //新开启一个进程
61        newThread(newRunnable() {
62           
63            @Override
64            publicvoidrun() {
65                // 循环1000次,不断地更新prostatus状态值
66                while(prostatus++<100000) {
67                    //将一个Runnable对象添加到消息队列中去
68                    //并且当执行该对象的时候,执行run
69                    handler.post(newRunnable() {
70                       
71                        @Override
72                        publicvoidrun() {
73                            //重新设置进度条当前的值
74                            progressbar.setProgress(prostatus);
75                            progressbar_1.setProgress(prostatus);
76                           
77                        }
78                    });    
79                }
80            }
81        }).start();
82       
83    }
84 
85//toast方法
86    privatevoidtoastshow(String str) {
87        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
88 
89    }
90 
91}

运行效果如下:


二:用图片实现滚动效果:

1.添加图片到drawable下

2.自定义图片资源文件iamge_progress.xml

1<?xmlversion="1.0"encoding="utf-8"?>
2<animated-rotate
3    xmlns:android="http://schemas.android.com/apk/res/android"
4    android:drawable="@drawable/image_progress"
5    android:pivotX="50%"
6    android:pivotY="50%"
7    />

3.定义布局文件,progress.xml

01<?xmlversion="1.0"encoding="utf-8"?>
02<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03android:orientation="vertical"
04android:layout_width="fill_parent"
05android:layout_height="fill_parent"
06android:gravity="center">
07<ProgressBar
08        android:indeterminateDrawable="@drawable/drawable_progress"
09        android:layout_height="100dp"
10        android:layout_width="100dp"/>
11</LinearLayout>

运行效果如下:

三》自定义渐变色进度条:

    定义drawable资源文件color_progressbar.xml

01<?xmlversion="1.0"encoding="utf-8"?>
02<layer-listxmlns:android="http://schemas.android.com/apk/res/android">
03   <itemandroid:id="@android:id/background">  
04    <shape>  
05        <cornersandroid:radius="5dip"/>  
06        <gradientandroid:startColor="#ff9d9e9d" 
07                android:centerColor="#ff5a5d5a" 
08                android:centerY="0.75" 
09                android:endColor="#ff747674" 
10                android:angle="270" 
11        />  
12    </shape>  
13</item>  
14<itemandroid:id="@android:id/secondaryProgress">  
15    <clip>  
16        <shape>  
17            <cornersandroid:radius="5dip"/>  
18            <gradientandroid:startColor="#80ffd300" 
19                    android:centerColor="#80ffb600" 
20                    android:centerY="0.75" 
21                    android:endColor="#a0ffcb00" 
22                    android:angle="270" 
23            />  
24        </shape>  
25    </clip>  
26</item>  
27<itemandroid:id="@android:id/progress" 
28>  
29    <clip>  
30        <shape>  
31            <cornersandroid:radius="5dip"/>  
32            <gradientandroid:startColor="#FF3030" 
33                android:endColor="#AEEEEE" 
34                android:angle="270"/>  
35        </shape>  
36    </clip>  
37</item>
38</layer-list>

2.定义对应的不布局文件:progressbar.xml在此文件中引用我们定义的drawable资源配置文件

01<?xmlversion="1.0"encoding="utf-8"?>
02<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
03android:orientation="vertical"
04android:layout_width="fill_parent"
05android:layout_height="fill_parent"
06android:gravity="center">
07<ProgressBar
08                android:id="@+id/color_progressBar"
09        android:indeterminateDrawable="@drawable/color_progress"
10        android:layout_height="wrap_content"
11        android:layout_width="match_parent"/>
12</LinearLayout>

或者在代码中给进度条设置自定义资源文件:

效果如下:

四:自定义progressbar颜色:

1.定义一个图片资源文件:

01<?xmlversion="1.0"encoding="utf-8"?> 
02<rotatexmlns:android="http://schemas.android.com/apk/res/android"
03    android:fromDegrees="0"
04    android:pivotX="50%"
05    android:pivotY="50%"
06    android:toDegrees="360"
07 
08    <shape
09        android:innerRadiusRatio="3"
10        android:shape="ring"
11        android:thicknessRatio="8"
12        android:useLevel="false"
13 
14        <gradient
15            android:centerColor="#FFFFFF"
16            android:centerY="0.50"
17            android:endColor="#FFFF00"
18            android:startColor="#000000"
19            android:type="sweep"
20            android:useLevel="false"/> 
21    </shape
22 
23</rotate>

2.定义布局文件:

1<ProgressBar
2        android:id="@+id/color_progressBar2"
3    android:indeterminateDrawable="@drawable/color_progress2"
4    android:layout_height="wrap_content"
5    android:layout_width="wrap_content"/>

3.效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值