Android 自定义谷歌风格ProgressBar(1),致Android开发者

这篇文章详细介绍了如何在Android应用中使用GoogleProgressDrawable构建可定制的进度条,包括设置颜色、速度、动画插值器等,并提供了applyStyle方法来应用预定义的样式。
摘要由CSDN通过智能技术生成

// 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(

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

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

我坚信,坚持学习,每天进步一点,滴水穿石,我们离成功都很近!
以下是总结出来的字节经典面试题目,包含:计算机网络,Kotlin,数据结构与算法,Framework源码,微信小程序,NDK音视频开发,计算机网络等。

字节高级Android经典面试题和答案


领取方法:

所有资料获取方式:评论666+点赞即可咨询资料免费领取方式!

直达领取链接:【Android高级架构师】文件夹下载!

9942138)]

最后

我坚信,坚持学习,每天进步一点,滴水穿石,我们离成功都很近!
以下是总结出来的字节经典面试题目,包含:计算机网络,Kotlin,数据结构与算法,Framework源码,微信小程序,NDK音视频开发,计算机网络等。

字节高级Android经典面试题和答案

[外链图片转存中…(img-O5uPtpDu-1711299942138)]
[外链图片转存中…(img-01hjY1WW-1711299942138)]

领取方法:

所有资料获取方式:评论666+点赞即可咨询资料免费领取方式!

直达领取链接:【Android高级架构师】文件夹下载!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值