https://github.com/ldoublem/RingProgress
2016-11-02 13:24
381 查看
很棒的创意,我还以为是外国人写的呢,厉害。
分析下
旋转的部分,RotateDrawable 其实已经抽象了,或者 setStartAngle 也可以做到。
SweepAngle 的部分已经实现过了,就是个映射。
多了一个设置 StrokeWidth 的接口。
public class RingDrawable extends Drawable {
// =================== RingDrawable ===================
public RingDrawable() {
initPaints();
}
// =================== paints ===================
private final Paint ringPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final RectF innerBounds = new RectF();
// =================== fields ===================
private int mRingWidth = 20;
private int mStartAngle = 0;
private int mSweepAngle = 360;
private double mProgress = 0;
private int[] mRingColors;
// todo please ignore this
public int inset = 0;
// =================== initPaints ===================
private void initPaints() {
ringPaint.setStyle(Paint.Style.STROKE);
ringPaint.setStrokeWidth(mRingWidth);
bgPaint.setStyle(Paint.Style.STROKE);
bgPaint.setStrokeWidth(mRingWidth);
}
// =================== onBoundChange ===================
@Override
protected void onBoundsChange(Rect bounds) {
calcInnerBounds();
}
private void calcInnerBounds() {
final int delta = mRingWidth / 2 + inset;
innerBounds.set(delta, delta, Math.max(0, getBounds().width() - delta), Math.max(0, getBounds().height() - delta));
}
// =================== drawProgress & drawBg ===================
// drawPath 不会出现 drawArc 的问题
private void drawProgress(@NonNull Canvas canvas) {
setRingShader();
Path ringPath = new Path();
ringPath.addArc(innerBounds, mStartAngle, mSweepAngle * getPercent());
canvas.drawPath(ringPath, ringPaint);
}
private void setRingShader() {
if (mRingColors == null) {
return;
}
int cx = getBounds().width() / 2;
int cy = getBounds().height() / 2;
Matrix matrix = new Matrix();
matrix.setRotate(mStartAngle, cx, cy);
SweepGradient shader = new SweepGradient(cx, cy, mRingColors, getPositions());
shader.setLocalMatrix(matrix);
ringPaint.setShader(shader);
}
private float[] getPositions() {
int length = mRingColors.length;
float[] positions = new float[length];
float delta = getPercent() * mSweepAngle / 360 / (length - 1);
for (int i = 0; i != length; i++) {
positions[i] = delta * i;
}
return positions;
}
private float getPercent() {
return (float) mProgress / 100;
}
private void drawBg(@NonNull Canvas canvas) {
Path bgPath = new Path();
bgPath.addArc(innerBounds, mStartAngle, mSweepAngle);
canvas.drawPath(bgPath, bgPaint);
}
// =================== setter ===================
public void setProgress(@FloatRange(from = 0.0, to = 100.0) float progress) {
mProgress = progress;
invalidateSelf();
}
public void setRingWidth(int ringWidth) {
mRingWidth = ringWidth;
onRingWidthChange();
}
private void onRingWidthChange() {
bgPaint.setStrokeWidth(mRingWidth);
ringPaint.setStrokeWidth(mRingWidth);
calcInnerBounds();
invalidateSelf();
}
public void setStartAngle(int startAngle) {
mStartAngle = startAngle;
invalidateSelf();
}
public void setSweepAngle(int sweepAngle) {
mSweepAngle = sweepAngle;
invalidateSelf();
}
public void setRingColors(int[] ringColors) {
mRingColors = ringColors;
}
public void setBgColor(@ColorInt int bgColor) {
bgPaint.setColor(bgColor);
invalidateSelf();
}
// =================== draw ===================
@Override
public void draw(@NonNull Canvas canvas) {
drawBg(canvas);
drawProgress(canvas);
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
其实最出彩的还是配色。
相关文章推荐
- https://github.com/lzyzsd/CircleProgress
- https://github.com/jakob-grabner/Circle-Progress-View
- https://github.com/tigerjj/ExpandedCircleProgressView
- Github: 团队账号:https://github.com/ChenRuTing
- git push时出现Username for 'https://github.com': 解决方法
- github FATAL:unable to access 'https://github.com/...: Failed to connect to github.com:443; No error
- fatal: could not read Username for 'https://github.com': No such file or directory
- MBProgressHUD框架的使用:https://github.com/jdg/MBProgressHUD
- https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/
- https://github.com/cykl/infoqscraper/
- iOS - 解决Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named
- https://github.com/gfto/mptsd
- Github错误:fatal: Authentication failed for 'https://github.com/ ...
- https://github.com/Trinea/android-open-project
- unable to access 'https://github.com/apache/incubator-rocketmq.git/': Could not resolve host: github
- https://github.com/1dot75cm/myrepo
- https://github.com/gaoyangxiaozhu/DockerVI
- APK动态加载框架 https://github.com/singwhatiwanna/dynamic-load-apk
- https://github.com/overboming/ZCAnimatedLabel