Android自定义谷歌风格ProgressBar
发布时间 - 2026-01-10 23:07:22 点击率:次本文实例为大家分享了谷歌风格ProgressBar的具体代码,供大家参考,具体内容如下
具体代码
package zms.demo.colorprogress;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ProgressBar;
public class GoogleProgressBar extends ProgressBar {
private static final int INTERPOLATOR_ACCELERATE = 0;
private static final int INTERPOLATOR_LINEAR = 1;
private static final int INTERPOLATOR_ACCELERATEDECELERATE = 2;
private static final int INTERPOLATOR_DECELERATE = 3;
public GoogleProgressBar(Context context) {
this(context, null);
}
public GoogleProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.googleProgressStyle);
}
public GoogleProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (isInEditMode()) {
setIndeterminateDrawable(new GoogleProgressDrawable.Builder(
context, true).build());
return;
}
Resources res = context.getResources();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.GoogleStyleProgressBar, defStyle, 0);
final int color = a.getColor(R.styleable.GoogleStyleProgressBar_color,
res.getColor(R.color.default_color));
final int sectionsCount = a.getInteger(
R.styleable.GoogleStyleProgressBar_sections_count,
res.getInteger(R.integer.default_sections_count));
final int separatorLength = a
.getDimensionPixelSize(
R.styleable.GoogleStyleProgressBar_stroke_separator_length,
res.getDimensionPixelSize(R.dimen.default_stroke_separator_length));
final float strokeWidth = a.getDimension(
R.styleable.GoogleStyleProgressBar_stroke_width,
res.getDimension(R.dimen.default_stroke_width));
final float speed = a.getFloat(
R.styleable.GoogleStyleProgressBar_speed,
Float.parseFloat(res.getString(R.string.default_speed)));
final float speedProgressiveStart = a.getFloat(
R.styleable.GoogleStyleProgressBar_progressiveStart_speed,
speed);
final float speedProgressiveStop = a
.getFloat(
R.styleable.GoogleStyleProgressBar_progressiveStop_speed,
speed);
final int iInterpolator = a.getInteger(
R.styleable.GoogleStyleProgressBar_interpolator, -1);
final boolean reversed = a.getBoolean(
R.styleable.GoogleStyleProgressBar_reversed,
res.getBoolean(R.bool.default_reversed));
final boolean mirrorMode = a.getBoolean(
R.styleable.GoogleStyleProgressBar_mirror_mode,
res.getBoolean(R.bool.default_mirror_mode));
final int colorsId = a.getResourceId(
R.styleable.GoogleStyleProgressBar_colors, 0);
final boolean progressiveStartActivated = a.getBoolean(
R.styleable.GoogleStyleProgressBar_progressiveStart_activated,
res.getBoolean(R.bool.default_progressiveStart_activated));
final Drawable backgroundDrawable = a
.getDrawable(R.styleable.GoogleStyleProgressBar_background);
final boolean generateBackgroundWithColors = a
.getBoolean(
R.styleable.GoogleStyleProgressBar_generate_background_with_colors,
false);
final boolean gradients = a.getBoolean(
R.styleable.GoogleStyleProgressBar_gradients, false);
a.recycle();
// interpolator
Interpolator interpolator = null;
if (iInterpolator == -1) {
interpolator = getInterpolator();
}
if (interpolator == null) {
switch (iInterpolator) {
case INTERPOLATOR_ACCELERATEDECELERATE:
interpolator = new AccelerateDecelerateInterpolator();
break;
case INTERPOLATOR_DECELERATE:
interpolator = new DecelerateInterpolator();
break;
case INTERPOLATOR_LINEAR:
interpolator = new LinearInterpolator();
break;
case INTERPOLATOR_ACCELERATE:
default:
interpolator = new AccelerateInterpolator();
}
}
int[] colors = null;
// colors
if (colorsId != 0) {
colors = res.getIntArray(colorsId);
}
GoogleProgressDrawable.Builder builder = new GoogleProgressDrawable.Builder(
context).speed(speed)
.progressiveStartSpeed(speedProgressiveStart)
.progressiveStopSpeed(speedProgressiveStop)
.interpolator(interpolator).sectionsCount(sectionsCount)
.separatorLength(separatorLength).strokeWidth(strokeWidth)
.reversed(reversed).mirrorMode(mirrorMode)
.progressiveStart(progressiveStartActivated)
.gradients(gradients);
if (backgroundDrawable != null) {
builder.backgroundDrawable(backgroundDrawable);
}
if (generateBackgroundWithColors) {
builder.generateBackgroundUsingColors();
}
if (colors != null && colors.length > 0)
builder.colors(colors);
else
builder.color(color);
GoogleProgressDrawable d = builder.build();
setIndeterminateDrawable(d);
}
public void applyStyle(int styleResId) {
TypedArray a = getContext().obtainStyledAttributes(null,
R.styleable.GoogleStyleProgressBar, 0, styleResId);
if (a.hasValue(R.styleable.GoogleStyleProgressBar_color)) {
setSmoothProgressDrawableColor(a.getColor(
R.styleable.GoogleStyleProgressBar_color, 0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_colors)) {
int colorsId = a.getResourceId(
R.styleable.GoogleStyleProgressBar_colors, 0);
if (colorsId != 0) {
int[] colors = getResources().getIntArray(colorsId);
if (colors != null && colors.length > 0)
setSmoothProgressDrawableColors(colors);
}
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_sections_count)) {
setSmoothProgressDrawableSectionsCount(a.getInteger(
R.styleable.GoogleStyleProgressBar_sections_count, 0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_stroke_separator_length)) {
setSmoothProgressDrawableSeparatorLength(a.getDimensionPixelSize(
R.styleable.GoogleStyleProgressBar_stroke_separator_length,
0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_stroke_width)) {
setSmoothProgressDrawableStrokeWidth(a.getDimension(
R.styleable.GoogleStyleProgressBar_stroke_width, 0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_speed)) {
setSmoothProgressDrawableSpeed(a.getFloat(
R.styleable.GoogleStyleProgressBar_speed, 0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStart_speed)) {
setSmoothProgressDrawableProgressiveStartSpeed(a.getFloat(
R.styleable.GoogleStyleProgressBar_progressiveStart_speed,
0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStop_speed)) {
setSmoothProgressDrawableProgressiveStopSpeed(a
.getFloat(
R.styleable.GoogleStyleProgressBar_progressiveStop_speed,
0));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_reversed)) {
setSmoothProgressDrawableReversed(a.getBoolean(
R.styleable.GoogleStyleProgressBar_reversed, false));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_mirror_mode)) {
setSmoothProgressDrawableMirrorMode(a.getBoolean(
R.styleable.GoogleStyleProgressBar_mirror_mode, false));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStart_activated)) {
setProgressiveStartActivated(a
.getBoolean(
R.styleable.GoogleStyleProgressBar_progressiveStart_activated,
false));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_progressiveStart_activated)) {
setProgressiveStartActivated(a
.getBoolean(
R.styleable.GoogleStyleProgressBar_progressiveStart_activated,
false));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_gradients)) {
setSmoothProgressDrawableUseGradients(a.getBoolean(
R.styleable.GoogleStyleProgressBar_gradients, false));
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_generate_background_with_colors)) {
if (a.getBoolean(
R.styleable.GoogleStyleProgressBar_generate_background_with_colors,
false)) {
setSmoothProgressDrawableBackgroundDrawable(GoogleProgressBarUtils
.generateDrawableWithColors(
checkIndeterminateDrawable().getColors(),
checkIndeterminateDrawable().getStrokeWidth()));
}
}
if (a.hasValue(R.styleable.GoogleStyleProgressBar_interpolator)) {
int iInterpolator = a.getInteger(
R.styleable.GoogleStyleProgressBar_interpolator, -1);
Interpolator interpolator;
switch (iInterpolator) {
case INTERPOLATOR_ACCELERATEDECELERATE:
interpolator = new AccelerateDecelerateInterpolator();
break;
case INTERPOLATOR_DECELERATE:
interpolator = new DecelerateInterpolator();
break;
case INTERPOLATOR_LINEAR:
interpolator = new LinearInterpolator();
break;
case INTERPOLATOR_ACCELERATE:
interpolator = new AccelerateInterpolator();
break;
default:
interpolator = null;
}
if (interpolator != null) {
setInterpolator(interpolator);
}
}
a.recycle();
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isIndeterminate()
&& getIndeterminateDrawable() instanceof GoogleProgressDrawable
&& !((GoogleProgressDrawable) getIndeterminateDrawable())
.isRunning()) {
getIndeterminateDrawable().draw(canvas);
}
}
private GoogleProgressDrawable checkIndeterminateDrawable() {
Drawable ret = getIndeterminateDrawable();
if (ret == null || !(ret instanceof GoogleProgressDrawable))
throw new RuntimeException(
"The drawable is not a SmoothProgressDrawable");
return (GoogleProgressDrawable) ret;
}
@Override
public void setInterpolator(Interpolator interpolator) {
super.setInterpolator(interpolator);
Drawable ret = getIndeterminateDrawable();
if (ret != null && (ret instanceof GoogleProgressDrawable))
((GoogleProgressDrawable) ret).setInterpolator(interpolator);
}
public void setSmoothProgressDrawableInterpolator(Interpolator interpolator) {
checkIndeterminateDrawable().setInterpolator(interpolator);
}
public void setSmoothProgressDrawableColors(int[] colors) {
checkIndeterminateDrawable().setColors(colors);
}
public void setSmoothProgressDrawableColor(int color) {
checkIndeterminateDrawable().setColor(color);
}
public void setSmoothProgressDrawableSpeed(float speed) {
checkIndeterminateDrawable().setSpeed(speed);
}
public void setSmoothProgressDrawableProgressiveStartSpeed(float speed) {
checkIndeterminateDrawable().setProgressiveStartSpeed(speed);
}
public void setSmoothProgressDrawableProgressiveStopSpeed(float speed) {
checkIndeterminateDrawable().setProgressiveStopSpeed(speed);
}
public void setSmoothProgressDrawableSectionsCount(int sectionsCount) {
checkIndeterminateDrawable().setSectionsCount(sectionsCount);
}
public void setSmoothProgressDrawableSeparatorLength(int separatorLength) {
checkIndeterminateDrawable().setSeparatorLength(separatorLength);
}
public void setSmoothProgressDrawableStrokeWidth(float strokeWidth) {
checkIndeterminateDrawable().setStrokeWidth(strokeWidth);
}
public void setSmoothProgressDrawableReversed(boolean reversed) {
checkIndeterminateDrawable().setReversed(reversed);
}
public void setSmoothProgressDrawableMirrorMode(boolean mirrorMode) {
checkIndeterminateDrawable().setMirrorMode(mirrorMode);
}
public void setProgressiveStartActivated(boolean progressiveStartActivated) {
checkIndeterminateDrawable().setProgressiveStartActivated(
progressiveStartActivated);
}
public void setSmoothProgressDrawableCallbacks(
GoogleProgressDrawable.Callbacks listener) {
checkIndeterminateDrawable().setCallbacks(listener);
}
public void setSmoothProgressDrawableBackgroundDrawable(Drawable drawable) {
checkIndeterminateDrawable().setBackgroundDrawable(drawable);
}
public void setSmoothProgressDrawableUseGradients(boolean useGradients) {
checkIndeterminateDrawable().setUseGradients(useGradients);
}
public void progressiveStart() {
checkIndeterminateDrawable().progressiveStart();
}
public void progressiveStop() {
checkIndeterminateDrawable().progressiveStop();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android
# ProgressBar
# Android自定义View 使用PathMeasure简单模仿系统ProgressBar(四)
# Android编程实现自定义ProgressBar样式示例(背景色及一级、二级进度条颜色)
# Android三种方式实现ProgressBar自定义圆形进度条
# Android编程ProgressBar自定义样式之动画模式实现方法
# android ListView和ProgressBar(进度条控件)的使用方法
# Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
# Android ProgressBar进度条使用详解
# Android编程之自定义ProgressBar示例
# 大家分享
# 具体内容
# 大家多多
# styleable
# GoogleStyleProgressBar
# color
# return
# getResources
# obtainStyledAttributes
# sectionsCount
# getInteger
# GoogleStyleProgressBar_sections_count
# getColor
# GoogleStyleProgressBar_color
# default_color
# googleProgressStyle
# defStyle
# super
# null
# attrs
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
教你用AI润色文章,让你的文字表达更专业
音乐网站服务器如何优化API响应速度?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
如何在阿里云购买域名并搭建网站?
Laravel事件和监听器如何实现_Laravel Events & Listeners解耦应用的实战教程
如何在七牛云存储上搭建网站并设置自定义域名?
如何破解联通资金短缺导致的基站建设难题?
如何快速搭建高效香港服务器网站?
如何用免费手机建站系统零基础打造专业网站?
使用C语言编写圣诞表白程序
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
如何快速生成ASP一键建站模板并优化安全性?
javascript日期怎么处理_如何格式化输出
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
google浏览器怎么清理缓存_谷歌浏览器清除缓存加速详细步骤
如何用PHP工具快速搭建高效网站?
Laravel模型事件有哪些_Laravel Model Event生命周期详解
Claude怎样写约束型提示词_Claude约束提示词写法【教程】
如何用景安虚拟主机手机版绑定域名建站?
Laravel distinct去重查询_Laravel Eloquent去重方法
活动邀请函制作网站有哪些,活动邀请函文案?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
JavaScript如何实现错误处理_try...catch如何捕获异常?
如何在Ubuntu系统下快速搭建WordPress个人网站?
Laravel如何使用.env文件管理环境变量?(最佳实践)
郑州企业网站制作公司,郑州招聘网站有哪些?
香港服务器如何优化才能显著提升网站加载速度?
Laravel如何创建自定义Artisan命令?(代码示例)
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
如何在Windows虚拟主机上快速搭建网站?
如何在云主机上快速搭建网站?
Win11关机界面怎么改_Win11自定义关机画面设置【工具】
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
Win11怎么设置默认图片查看器_Windows11照片应用关联设置
C++用Dijkstra(迪杰斯特拉)算法求最短路径
详解CentOS6.5 安装 MySQL5.1.71的方法
Laravel怎么使用Markdown渲染文档_Laravel将Markdown内容转HTML页面展示【实战】
,怎么在广州志愿者网站注册?
如何为不同团队 ID 动态生成多个“认领值班”按钮
Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧
Windows Hello人脸识别突然无法使用
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
Linux虚拟化技术教程_KVMQEMU虚拟机安装与调优
如何快速查询网址的建站时间与历史轨迹?
Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
如何用ChatGPT准备面试 模拟面试问答与职场话术练习教程
Laravel怎么使用Session存储数据_Laravel会话管理与自定义驱动配置【详解】

