Android踩坑日记:自定义水平和圆形ProgressBar样式

2024-04-26 11:58

本文主要是介绍Android踩坑日记:自定义水平和圆形ProgressBar样式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

自定义水平和圆形ProgressBar样式

1.自定义水平ProgressBar样式

  • ProgressBar分为两种,我们能明确看到进度,不确定的就是不清楚、不确定一个操作需要多长时间来完成,这个时候就需要用的不确定的ProgressBar了。
  • ProgressBar(Horizontal 才有,无进度的没有)有两个进度,一个是android:progress,另一个是android:secondaryProgress。后者主要是为缓存需要所涉及的,比如在看网络视频时候都会有一个缓存的进度条以及还要一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。

1.ProgressBar的样式设定其实有两种方式,在API文档中说明的方式如下:

  • Widget.ProgressBar.Horizontal
  • Widget.ProgressBar.Small
  • Widget.ProgressBar.Large
  • Widget.ProgressBar.Inverse
  • Widget.ProgressBar.Small.Inverse
  • Widget.ProgressBar.Large.Inverse


使用的时候可以这样:style=”@android:style/Widget.ProgressBar.Horizontal”
另外还有一种方式就是使用系统的attr,下面的方式是系统的style:

  • style=”?android:attr/progressBarStyle”
  • style=”?android:attr/progressBarStyleHorizontal”
  • style=”?android:attr/progressBarStyleInverse”
  • style=”?android:attr/progressBarStyleLarge”
  • style=”?android:attr/progressBarStyleLargeInverse”
  • style=”?android:attr/progressBarStyleSmall”
  • style=”?android:attr/progressBarStyleSmallInverse”
  • style=”?android:attr/progressBarStyleSmallTitle”

比如:

<ProgressBar
    android:id="@+id/progressBar"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="match_parent"android:layout_height="wrap_content" />
ProgressBarandroid:id="@+id/progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content" />

我们去看看style=”@android:style/Widget.ProgressBar.Horizontal” 的源码

 <style name="Widget.ProgressBar.Horizontal"><item name="indeterminateOnly">false</item><!-- 进度条的背景,progress ,secondaryProgress 的颜色--><item name="progressDrawable">@drawable/progress_horizontal</item><item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item><item name="minHeight">20dip</item><item name="maxHeight">20dip</item><item name="mirrorForRtl">true</item></style>

下面看@android:drawable/progress_horizontal的源码

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source ProjectLicensed 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations 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>

三个条目,对应了background,progress,secondaryProgress。所以只需要修改对应项就可以了,如下在drawable文件中创建一个 progressbar_horizontal_custom.xml
所以其实我们要修改样式的话只需要修改android:progressDrawable”对应的资源就可以

<item name="android:progressDrawable">@drawable/progressbar_horizontal</item><!-- progress_horizontal -->

video_view_bottom_progressbar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@android:id/background"android:height="2dp"android:gravity="center"><shape><size android:height="2dp"/><corners android:radius="1dp" /><solid android:color="#dcdcdc"/></shape></item><item android:id="@android:id/secondaryProgress"android:height="2dp"android:gravity="center"><clip ><shape><size android:height="2dp"/><corners android:radius="5dp" /><solid android:color="@android:color/darker_gray"/><!--darker_gray--></shape></clip></item><item android:id="@android:id/progress"android:height="2dp"android:gravity="center"><clip ><shape><size android:height="2dp"/><corners android:radius="5dp" /><solid android:color="#FF455B"/></shape></clip></item>
</layer-list>

在布局文件xml中使用:

<ProgressBarandroid:id="@+id/pb_video_bottom_progress"style="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:max="100"android:maxHeight="2dp"android:minHeight="2dp"android:progressDrawable="@drawable/video_view_bottom_progressbar_background"android:visibility="visible" />

总结:

1. 使用style=”@android:style/Widget.ProgressBar.Horizontal”
2. 重新自自定android:progressDrawable

2.自定义圆形ProgressBar样式


比如

<ProgressBar
    android:id="@+id/progressBar"style="@android:style/Widget.ProgressBar.Small"android:layout_gravity="center_vertical"android:layout_width="match_parent"android:layout_height="wrap_content" />

我们先看Widget.Progress的源码:

<style name="Widget.ProgressBar.Small"><!--圆形背景--><item name="indeterminateDrawable">@drawable/progress_small_white</item><item name="minWidth">16dip</item><item name="maxWidth">16dip</item><item name="minHeight">16dip</item><item name="maxHeight">16dip</item></style>

@drawable/progress_small_white的源码:

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@drawable/spinner_white_48"android:pivotX="50%"android:pivotY="50%"android:framesCount="12"android:frameDuration="100" />

发现其实就是一个不断重复的旋转动画是spinner_white_48的图片,所以我们只需要自定义name=”indeterminateDrawable”的属性,自定义自己的drawable使用animated-rotate 标签即可
总结:
1.修改indeterminateDrawable属性,编写animated-rotate 标签的drawable,替换系统的

这篇关于Android踩坑日记:自定义水平和圆形ProgressBar样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/937524

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

Python清空Word段落样式的三种方法

《Python清空Word段落样式的三种方法》:本文主要介绍如何用python-docx库清空Word段落样式,提供三种方法:设置为Normal样式、清除直接格式、创建新Normal样式,注意需重... 目录方法一:直接设置段落样式为"Normal"方法二:清除所有直接格式设置方法三:创建新的Normal样

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (