Android编程实现自定义进度条颜色的方法
作者:sengeiou 发布时间:2023-07-24 07:40:58
本文实例讲述了Android编程实现自定义进度条颜色的方法。分享给大家供大家参考,具体如下:
android 自定义进度条颜色
先看图
基于产品经理各种自定义需求,经过查阅了解,下面是自己对android自定义进度条的学习过程!
这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\ 下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找styles.xml
找到xml后,进去找到
<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,但今天我们修改的是水平进度条颜色!
所以找到
<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 文件,改变他就可以改变进度条颜色
<?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>
<?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>
看到没有,这是系统的进度条画出的布局条件
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:endColor="#ff747674"
我们只要改变这个色值就能改变他的颜色,主要改变的是<item android:id="@android:id/progress">下的色值就行了
说了这么多,到底怎么做呢, 很简单
1. 在我们的项目下新建一个 style.xml 文件
创建一个style 标签,集成系统默认样式,然后自定义一个新的progressDrawable 文件,随后面在layout 中的progress 中引用这个文件就行
<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" 的色值
<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>
最后在中引用就可以了
<ProgressBar
android:id="@+id/progress"
style="@style/ProgressBar_Mini"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="50" />
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- BufferedReader读取文件指定字符集问题默认的读取方式BufferedReader bufferedReader = new Bu
- 判断有无虚拟按键(导航栏)现在很大一部分手机没有虚拟按键,一部分有。我们在做适配的时候可能会用到这方面的知识。例如:屏幕填充整个屏幕的时候,
- 一、引入maven依赖Spring Boot默认使用LogBack,但是我们没有看到显示依赖的jar包,其实是因为所在的jar包spring
- 本文实例讲述了C#使用windows服务开启应用程序的方法。分享给大家供大家参考。具体如下:使用windows服务开启应用程序,会遇到如下问
- 本文实例为大家分享了Java实现猜拳游戏的具体代码,供大家参考,具体内容如下一、问题简介通过控制台方式实现一个人机对战的猜拳游戏,用户通过输
- 首先要引用一下类库:using Ionic.Zip;这个类库可以到网上下载。下面对类库使用的封装方法:得到指定的输入流的ZIP压
- 排查@CacheEvict注解失效我简单看了一下《Spring实战》中的demo,然后就应用到业务代码中了,本以为如此简单的事情,竟然在代码
- Retrofit 基本使用implementation 'com.squareup.retrofit2:retrofit:2.9.0
- 本文实例讲述了Android中资源文件用法。分享给大家供大家参考,具体如下:一、XML文件间资源文件的使用引用格式:attribute=&q
- 一、项目概述本次项目主要实现了天气预报功能。通过调用天气预报接口来获得天气数据,用LIstView和GridView来搭建每个界面,将查询的
- 1、redis的几种常见客户端:Jedis:是Redis的Java实现客户端,提供了比较全面的Redis命令的支持;Redisson:实现了
- 这篇文章主要介绍了spring cloud alibaba Nacos 注册中心搭建过程详解,文中通过示例代码介绍的非常详细,对大家的学习或
- 其实如果我们不进行设置,只是修改了代码,运行程序以后,其出错界面如下图1所示:图1抛出异常如下:************** Excepti
- BufferedInputStream BufferedInputStream 是缓冲输入流。它继承于FilterInputSt
- 本文实例为大家分享了Android实现串口通信的具体代码,供大家参考,具体内容如下生成so文件首先确保已经安装了NDK和CMake然后创建一
- 本文实例讲述了C++二分查找(折半查找)算法。分享给大家供大家参考,具体如下:二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好
- 1、synchronized的作用为了避免临界区的竞态条件发生,有多种手段可以达到目的。阻塞式的解决方案:synchronized,Lock
- 环境JDK 1.8Spring Boot 2.3.0.RELEASEMaven 3.6.1H2 数据库用户名密码登录首先,我们用 Sprin
- 什么是构建生命周期构建生命周期是一组阶段的序列(sequence of phases),这些构建生命周期中的每一个由构建阶段的不同列表定义,
- 用来记录自己所用到的知识前两天在做项目的时候发现有时候在访问网络数据的时候由于后台要做的工作较多,给我们返回数据的时间较长,所以老大叫我加了