Android 自定义谷歌风格ProgressBar

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) {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

[外链图片转存中…(img-T9WtQEfL-1715366499750)]

[外链图片转存中…(img-GYetBbhv-1715366499752)]

[外链图片转存中…(img-vETPAOLS-1715366499754)]

[外链图片转存中…(img-4ywQWXgH-1715366499755)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值