服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android编程实现自定义进度条颜色的方法

Android编程实现自定义进度条颜色的方法

2021-04-15 15:30sengeiou Android

这篇文章主要介绍了Android编程实现自定义进度条颜色的方法,涉及Android进度条的样式布局及功能实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android编程实现自定义进度条颜色的方法。分享给大家供大家参考,具体如下:

android 自定义进度条颜色

先看图

Android编程实现自定义进度条颜色的方法

基于产品经理各种自定义需求,经过查阅了解,下面是自己对android自定义进度条的学习过程!

这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\  下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找styles.xml
找到xml后,进去找到

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<style name="widget.progressbar">
    <item name="android:indeterminateonly">true</item>
    <item name="android:indeterminatedrawable">@android:drawable/progress_medium_white</item>
    <item name="android:indeterminatebehavior">repeat</item>
    <item name="android:indeterminateduration">3500</item>
    <item name="android:minwidth">48dip</item>
    <item name="android:maxwidth">48dip</item>
    <item name="android:minheight">48dip</item>
    <item name="android:maxheight">48dip</item>
</style>
<style name="widget.progressbar">
    <item name="android:indeterminateonly">true</item>
    <item name="android:indeterminatedrawable">@android:drawable/progress_medium_white</item>
    <item name="android:indeterminatebehavior">repeat</item>
    <item name="android:indeterminateduration">3500</item>
    <item name="android:minwidth">48dip</item>
    <item name="android:maxwidth">48dip</item>
    <item name="android:minheight">48dip</item>
    <item name="android:maxheight">48dip</item>
</style>

这是默认转圈的效果style,但今天我们修改的是水平进度条颜色!
所以找到

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<style name="widget.progressbar.horizontal">
    <item name="android:indeterminateonly">false</item>
    <item name="android:progressdrawable">@android:drawable/progress_horizontal</item>
    <item name="android:indeterminatedrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <item name="android:minheight">20dip</item>
    <item name="android:maxheight">20dip</item>
</style>
<style name="widget.progressbar.horizontal">
    <item name="android:indeterminateonly">false</item>
    <item name="android:progressdrawable">@android:drawable/progress_horizontal</item>
    <item name="android:indeterminatedrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <item name="android:minheight">20dip</item>
    <item name="android:maxheight">20dip</item>
</style>

你看系统一步一步关联的,扩展性很性,低耦合,所以我们现在只要改变进度条是怎么样画出来的就行了 ,但是负责画进度条的是
<item name="android:progressdrawable">  所以我们可以找到"drawable下的 progress_horizontal 文件,改变他就可以改变进度条颜色

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<!-- copyright (c) 2008 the android open source project
   licensed under the apache license, version 2.0 (the "license");
   you may not use this file except in compliance with the license.
   you may obtain a copy of the license at
     http://www.apache.org/licenses/license-2.0
   unless required by applicable law or agreed to in writing, software
   distributed under the license is distributed on an "as is" basis,
   without warranties or conditions of any kind, either express or implied.
   see the license for the specific language governing permissions and
   limitations under the license.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@android:id/background">
    <shape>
      <corners android:radius="5dip" />
      <gradient
          android:startcolor="#ff9d9e9d"
          android:centercolor="#ff5a5d5a"
          android:centery="0.75"
          android:endcolor="#ff747674"
          android:angle="270"
      />
    </shape>
  </item>
  <item android:id="@android:id/secondaryprogress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startcolor="#80ffd300"
            android:centercolor="#80ffb600"
            android:centery="0.75"
            android:endcolor="#a0ffcb00"
            android:angle="270"
        />
      </shape>
    </clip>
  </item>
  <item android:id="@android:id/progress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startcolor="#ffffd300"
            android:centercolor="#ffffb600"
            android:centery="0.75"
            android:endcolor="#ffffcb00"
            android:angle="270"
        />
      </shape>
    </clip>
  </item>
</layer-list>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<!-- copyright (c) 2008 the android open source project
   licensed under the apache license, version 2.0 (the "license");
   you may not use this file except in compliance with the license.
   you may obtain a copy of the license at
     http://www.apache.org/licenses/license-2.0
   unless required by applicable law or agreed to in writing, software
   distributed under the license is distributed on an "as is" basis,
   without warranties or conditions of any kind, either express or implied.
   see the license for the specific language governing permissions and
   limitations under the license.
-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@android:id/background">
    <shape>
      <corners android:radius="5dip" />
      <gradient
          android:startcolor="#ff9d9e9d"
          android:centercolor="#ff5a5d5a"
          android:centery="0.75"
          android:endcolor="#ff747674"
          android:angle="270"
      />
    </shape>
  </item>
  <item android:id="@android:id/secondaryprogress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startcolor="#80ffd300"
            android:centercolor="#80ffb600"
            android:centery="0.75"
            android:endcolor="#a0ffcb00"
            android:angle="270"
        />
      </shape>
    </clip>
  </item>
  <item android:id="@android:id/progress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startcolor="#ffffd300"
            android:centercolor="#ffffb600"
            android:centery="0.75"
            android:endcolor="#ffffcb00"
            android:angle="270"
        />
      </shape>
    </clip>
  </item>
</layer-list>

看到没有,这是系统的进度条画出的布局条件

?
1
2
3
android:startcolor="#80ffd300"
android:centercolor="#80ffb600"
android:endcolor="#ff747674"

我们只要改变这个色值就能改变他的颜色,主要改变的是<item android:id="@android:id/progress">下的色值就行了
说了这么多,到底怎么做呢, 很简单

1. 在我们的项目下新建一个 style.xml 文件

创建一个style 标签,集成系统默认样式,然后自定义一个新的progressdrawable  文件,随后面在layout 中的progress 中引用这个文件就行

?
1
2
3
4
5
6
7
<style name="progressbar_mini" parent="@android:style/widget.progressbar.horizontal">
    <item name="android:maxheight">50dip</item>
    <item name="android:minheight">8dip</item>
    <item name="android:indeterminateonly">false</item>
    <item name="android:indeterminatedrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <item name="android:progressdrawable">@drawable/progressbar_mini</item>
</style>

下面是我的  progressbar_mini  文件,改变了下android:endcolor="#f5f5f5" android:startcolor="#bebebe"  的色值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:id="@android:id/background">
     <shape >
        <corners android:radius="5dip" />
        <gradient
          android:angle="270"
          android:centery="0.75"
          android:endcolor="#f5f5f5"
          android:startcolor="#bebebe" />
      </shape>
  </item>
  <item android:id="@android:id/secondaryprogress">
    <clip >
      <shape >
        <corners android:radius="0dip" />
        <gradient
          android:angle="270"
          android:centery="0.75"
          android:endcolor="#165cbc"
          android:startcolor="#85b0e9" />
      </shape>
    </clip>
  </item>
  <item android:id="@android:id/progress">
    <clip >
      <shape >
        <corners android:radius="5dip" />
        <gradient
          android:angle="270"
          android:centery="0.75"
          android:endcolor="#165cbc"
          android:startcolor="#85b0e9" />
      </shape>
    </clip>
  </item>
</layer-list>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:id="@android:id/background">
     <shape >
        <corners android:radius="5dip" />
        <gradient
          android:angle="270"
          android:centery="0.75"
          android:endcolor="#f5f5f5"
          android:startcolor="#bebebe" />
      </shape>
  </item>
  <item android:id="@android:id/secondaryprogress">
    <clip >
      <shape >
        <corners android:radius="0dip" />
        <gradient
          android:angle="270"
          android:centery="0.75"
          android:endcolor="#165cbc"
          android:startcolor="#85b0e9" />
      </shape>
    </clip>
  </item>
  <item android:id="@android:id/progress">
    <clip >
      <shape >
        <corners android:radius="5dip" />
        <gradient
          android:angle="270"
          android:centery="0.75"
          android:endcolor="#165cbc"
          android:startcolor="#85b0e9" />
      </shape>
    </clip>
  </item>
</layer-list>

最后在中引用就可以了

?
1
2
3
4
5
6
<progressbar
    android:id="@+id/progress"
    style="@style/progressbar_mini"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:progress="50" />

Android编程实现自定义进度条颜色的方法

希望本文所述对大家android程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28