1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
public class CircularProgressView extends View {
private Paint mBackPaint, mProgPaint; private RectF mRectF; private int[] mColorArray; private int mProgress;
public CircularProgressView(Context context) { this(context, null); }
public CircularProgressView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }
public CircularProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularProgressView);
mBackPaint = new Paint(); mBackPaint.setStyle(Paint.Style.STROKE); mBackPaint.setStrokeCap(Paint.Cap.ROUND); mBackPaint.setAntiAlias(true); mBackPaint.setDither(true); mBackPaint.setStrokeWidth(typedArray.getDimension(R.styleable.CircularProgressView_backWidth, 5)); mBackPaint.setColor(typedArray.getColor(R.styleable.CircularProgressView_backColor, Color.LTGRAY));
mProgPaint = new Paint(); mProgPaint.setStyle(Paint.Style.STROKE); mProgPaint.setStrokeCap(Paint.Cap.ROUND); mProgPaint.setAntiAlias(true); mProgPaint.setDither(true); mProgPaint.setStrokeWidth(typedArray.getDimension(R.styleable.CircularProgressView_progWidth, 10)); mProgPaint.setColor(typedArray.getColor(R.styleable.CircularProgressView_progColor, Color.BLUE));
int startColor = typedArray.getColor(R.styleable.CircularProgressView_progStartColor, -1); int firstColor = typedArray.getColor(R.styleable.CircularProgressView_progFirstColor, -1); if (startColor != -1 && firstColor != -1) mColorArray = new int[]{startColor, firstColor}; else mColorArray = null;
mProgress = typedArray.getInteger(R.styleable.CircularProgressView_progress, 0); typedArray.recycle(); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int viewWide = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); int viewHigh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); int mRectLength = (int) ((viewWide > viewHigh ? viewHigh : viewWide) - (mBackPaint.getStrokeWidth() > mProgPaint.getStrokeWidth() ? mBackPaint.getStrokeWidth() : mProgPaint.getStrokeWidth())); int mRectL = getPaddingLeft() + (viewWide - mRectLength) / 2; int mRectT = getPaddingTop() + (viewHigh - mRectLength) / 2; mRectF = new RectF(mRectL, mRectT, mRectL + mRectLength, mRectT + mRectLength);
if (mColorArray != null && mColorArray.length > 1) mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR)); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawArc(mRectF, 0, 360, false, mBackPaint); canvas.drawArc(mRectF, 275, 360 * mProgress / 100, false, mProgPaint); }
public int getProgress() { return mProgress; }
public void setProgress(int progress) { this.mProgress = progress; invalidate(); }
public void setProgress(int progress, long animTime) { if (animTime <= 0) setProgress(progress); else { ValueAnimator animator = ValueAnimator.ofInt(mProgress, progress); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mProgress = (int) animation.getAnimatedValue(); invalidate(); } }); animator.setInterpolator(new OvershootInterpolator()); animator.setDuration(animTime); animator.start(); } }
public void setBackWidth(int width) { mBackPaint.setStrokeWidth(width); invalidate(); }
public void setBackColor(@ColorRes int color) { mBackPaint.setColor(ContextCompat.getColor(getContext(), color)); invalidate(); }
public void setProgWidth(int width) { mProgPaint.setStrokeWidth(width); invalidate(); }
public void setProgColor(@ColorRes int color) { mProgPaint.setColor(ContextCompat.getColor(getContext(), color)); mProgPaint.setShader(null); invalidate(); }
public void setProgColor(@ColorRes int startColor, @ColorRes int firstColor) { mColorArray = new int[]{ContextCompat.getColor(getContext(), startColor), ContextCompat.getColor(getContext(), firstColor)}; mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR)); invalidate(); }
public void setProgColor(@ColorRes int[] colorArray) { if (colorArray == null || colorArray.length < 2) return; mColorArray = new int[colorArray.length]; for (int index = 0; index < colorArray.length; index++) mColorArray[index] = ContextCompat.getColor(getContext(), colorArray[index]); mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR)); invalidate(); } }
|