ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android 自定义谷歌风格ProgressBar,鸿蒙app开发工具

2022-03-19 21:05:15  阅读:216  来源: 互联网

标签:GoogleStyleProgressBar 自定义 app styleable ProgressBar colors interpolator Android


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

setSmoothPr

ogressDrawableProgressiveStopSpeed(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()

最后

在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

a.recycle();

}

@Override

protected synchronized void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (isIndeterminate()

最后

在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-eFcMvk8L-1647694627171)]

标签:GoogleStyleProgressBar,自定义,app,styleable,ProgressBar,colors,interpolator,Android
来源: https://blog.csdn.net/m0_67735420/article/details/123602552

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有