import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
+import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
+import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
+import android.view.ViewGroup;
+import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
+import androidx.annotation.ColorInt;
+import androidx.annotation.Nullable;
+import androidx.core.content.ContextCompat;
+
import com.ic.sport.R;
+import com.ic.sport.util.AndroidUtils;
-import java.util.ArrayList;
-import java.util.List;
public class RingProgress extends View {
+ public boolean isDebug = true;
+ private OnRingProgressInter onRingProgressInter;
- private Paint mPaint;
- private Bitmap mBitmapBg;
- private Paint mPaintText;
- private int SweepAngle = 180;
- private int mPadding;
- private int mWidth;
-
- private int ringWidth = 0;
-
-
- private int rotateAngle = 270;
-
- private int bgShadowColor = Color.argb(100, 0, 0, 0);
- private int bgColor = Color.rgb(141, 141, 141);
-
- private List<Ring> mListRing = new ArrayList<>();
- private RectF rectFBg = new RectF();
- private boolean isCorner = true;
- private boolean isDrawBg = true;
- private boolean isDrawBgShadow = true;
- private float ringWidthScale = 0f;
- private boolean bgChange = false;
-
- public int getSweepAngle() {
- return SweepAngle;
- }
-
- public void setSweepAngle(int sweepAngle) {
-
- if (sweepAngle < 0)
- sweepAngle = 0;
- else if (sweepAngle > 360)
- sweepAngle = 360;
- SweepAngle = sweepAngle;
- bgChange = true;
- invalidate();
- }
-
- public int getRotateAngle() {
- return rotateAngle;
+ public interface OnRingProgressInter {
+ void progress(float scaleProgress, float progress, float max);
}
- public void setRotateAngle(int rotateAngle) {
-
- if (rotateAngle < 0)
- rotateAngle = 0;
- else if (rotateAngle > 360)
- rotateAngle = 360;
-
+ private int centerX;
+ private int centerY;
+ private Paint mPaint;
+ private Shader progressShader;
+// private float viewWidth;
+// private float viewHeight;
+
+ //内圆颜色
+ private int neiYuanColor;
+ //圆环半径
+ private int ringRadius;
+ //圆环宽度
+ private int ringWidth;
+ //圆环颜色
+ private int ringColor;
+ // 圆环进度颜色
+ private int ringProgressColor;
+ //圆环进度过度颜色
+// private int ringProgressSecondColor;
+ //开始角度
+ private int startAngle = -45;
+ //是否顺时针
+ private boolean isClockwise = true;
+ //当前进度
+ private float progress = 10;
+ //用于执行动画时的进度回调
+ private float scaleProgress = progress;
+ //总进度
+ private float maxProgress = 1000;
+
+ //用于逻辑计算的总进度(主要使动画效果更平滑)
+ private final int viewMax = 3600;
+ //用于逻辑计算的当前进度(主要使动画效果更平滑)
+ private float viewProgress = progress * viewMax / maxProgress;
+
+ //不绘制的度数
+ private int disableAngle = 0;
+ //圆环进度是否为圆角
+ private boolean isRound = true;
+ //是否设置动画
+ private boolean useAnimation = true;
+ //动画执行时间
+ private int duration = 1000;
+
+ //进度百分比
+ private double progressPercent;
+ //进度百分比数值是否是小数
+ private boolean isDecimal = true;
+ //小数点后几位
+ private int decimalPointLength = 1;
+ //是否显示百分比
+ private boolean isShowPercentText = false;
+ //文字颜色
+ private int textColor;
+ //文字大小
+ private int textSize;
- this.rotateAngle = rotateAngle;
- invalidate();
+ public RingProgress(Context context) {
+ super(context);
+ initAttr(null);
}
- public boolean isCorner() {
- return isCorner;
+ public RingProgress(Context context, @Nullable AttributeSet attrs) {
+ super(context, attrs);
+ initAttr(attrs);
}
- public void setCorner(boolean corner) {
- isCorner = corner;
- bgChange = true;
- invalidate();
+ public RingProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ initAttr(attrs);
}
- public boolean isDrawBgShadow() {
- return isDrawBgShadow;
- }
+ private void initAttr(AttributeSet attrs) {
+ initData();
+ initPaint();
+ if (attrs == null) {
+ return;
+ }
+ TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CircleProgress);
+ neiYuanColor = typedArray.getColor(R.styleable.CircleProgress_neiYuanColor, getTransparentColor());
+ ringRadius = (int) typedArray.getDimension(R.styleable.CircleProgress_ringRadius, -1);
+ ringWidth = (int) typedArray.getDimension(R.styleable.CircleProgress_ringWidth, 30);
+ ringColor = typedArray.getColor(R.styleable.CircleProgress_ringColor,
+ ContextCompat.getColor(getContext(), R.color.top_color2));
+ ringProgressColor = typedArray.getColor(R.styleable.CircleProgress_ringProgressColor,
+ ContextCompat.getColor(getContext(), R.color.green1));
+ startAngle = typedArray.getInteger(R.styleable.CircleProgress_startAngle, 135);
+ isClockwise = typedArray.getBoolean(R.styleable.CircleProgress_isClockwise, false);
+ progress = typedArray.getFloat(R.styleable.CircleProgress_progress, 10);
+ maxProgress = typedArray.getFloat(R.styleable.CircleProgress_maxProgress, 100);
+
+ if (maxProgress <= 0) {
+ this.maxProgress = 0;
+ }
+ if (progress > maxProgress) {
+ this.progress = maxProgress;
+ } else if (progress < 0) {
+ this.progress = 0;
+ }
- public void setDrawBgShadow(boolean drawBgShadow) {
- isDrawBgShadow = drawBgShadow;
- bgChange = true;
- invalidate();
+ scaleProgress = progress;
+ viewProgress = progress * viewMax / maxProgress;
+
+ disableAngle = typedArray.getInteger(R.styleable.CircleProgress_disableAngle, 90);
+ isRound = typedArray.getBoolean(R.styleable.CircleProgress_isRound, true);
+ useAnimation = typedArray.getBoolean(R.styleable.CircleProgress_useAnimation, true);
+ duration = typedArray.getInteger(R.styleable.CircleProgress_duration, 1000);
+ isDecimal = typedArray.getBoolean(R.styleable.CircleProgress_isDecimal, true);
+ decimalPointLength = typedArray.getInteger(R.styleable.CircleProgress_decimalPointLength, 1);
+ isShowPercentText = typedArray.getBoolean(R.styleable.CircleProgress_isShowPercentText, true);
+ textColor = typedArray.getColor(R.styleable.CircleProgress_textColor, ContextCompat.getColor(getContext(), R.color.green1));
+ textSize = (int) typedArray.getDimension(R.styleable.CircleProgress_textSize, getDef_TextSize());
+ typedArray.recycle();
+// initData();
}
- public void setDrawBgShadow(boolean drawBgShadow, int color) {
- isDrawBgShadow = drawBgShadow;
- this.bgShadowColor = color;
- bgChange = true;
- invalidate();
+ private int getTransparentColor() {
+ return ContextCompat.getColor(getContext(), R.color.transparent);
}
-
- public boolean isDrawBg() {
- return isDrawBg;
+ private int getDef_TextSize() {
+ return dip2px(getContext(), 17);
}
- public void setDrawBg(boolean drawBg) {
- isDrawBg = drawBg;
- bgChange = true;
- invalidate();
+ private void initPaint() {
+ mPaint = new Paint();
+ mPaint.setAntiAlias(true);
+ mPaint.setDither(true);
+ mPaint.setColor(Color.WHITE);
+ mPaint.setStyle(Paint.Style.FILL);
}
- public void setDrawBg(boolean drawBg, int color) {
- isDrawBg = drawBg;
- this.bgColor = color;
- bgChange = true;
- invalidate();
- }
- public List<Ring> getmListRing() {
- return mListRing;
+ private void initData() {
+ neiYuanColor = ContextCompat.getColor(getContext(), R.color.transparent);
+ ringRadius = -1;
+ ringWidth = 30;
+ ringColor = ContextCompat.getColor(getContext(), R.color.top_color2);
+ ringProgressColor = ContextCompat.getColor(getContext(), R.color.green1);
+ textSize = dip2px(getContext(), 17);
+ textColor = ContextCompat.getColor(getContext(), R.color.green1);
}
- public void setmListRing(List<Ring> mListRing) {
- this.mListRing = mListRing;
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+ int widthSize = MeasureSpec.getSize(widthMeasureSpec);
+ int heightSize = MeasureSpec.getSize(heightMeasureSpec);
+ int mWidth = 200;
+ int mHeight = 200;
+ if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
+ setMeasuredDimension(mWidth, mHeight);
+ } else if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
+ setMeasuredDimension(mWidth, heightSize);
+ } else if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) {
+ setMeasuredDimension(widthSize, mHeight);
+ }
}
+ /* @Override
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+ super.onSizeChanged(w, h, oldw, oldh);
+ viewWidth=w;
+ viewHeight=h;
+ }*/
- public float getRingWidthScale() {
- return ringWidthScale;
- }
-
- public void setRingWidthScale(float ringWidthScale) {
- this.ringWidthScale = ringWidthScale;
- bgChange = true;
- invalidate();
+ @Override
+ protected void onDraw(Canvas canvas) {
+ super.onDraw(canvas);
+ int WH = Math.min(getWidth() - getPaddingLeft()
+ - getPaddingRight(),
+ getHeight() - getPaddingTop() - getPaddingBottom());
+ if (ringRadius < 0) {
+ ringRadius = (WH - ringWidth) / 2 - 5;
+ }
+ centerX = getWidth() / 2;
+ centerY = getHeight() / 2;
+ //绘制圆环
+ //drawRing(canvas);
+ drawRing2(canvas);
+ //绘制内圆
+ drawNeiYuan(canvas);
+ //绘制进度圆环
+ drawProgressRing(canvas);
+ //绘制进度百分比
+ if (isShowPercentText) {
+ drawProgressText(canvas);
+ }
}
-
- public void setData(List<Ring> mListRing, int time) {
- this.mListRing.clear();
- for (int i = 0; i < mListRing.size(); i++) {
-
- RectF r = new RectF();
- r.top = rectFBg.top + ringWidth * i;
- r.bottom = rectFBg.bottom - ringWidth * i;
- r.left = rectFBg.left + ringWidth * i;
- r.right = rectFBg.right - ringWidth * i;
- mListRing.get(i).setRectFRing(r);
+ private void drawProgressText(Canvas canvas) {
+ mPaint.reset();
+ mPaint.setAntiAlias(true);
+ mPaint.setDither(true);
+ progressPercent = AndroidUtils.chuFa(progress * 100, maxProgress, decimalPointLength);
+ String percentStr = progressPercent + "%";
+ if (!isDecimal) {
+ percentStr = ((int) progressPercent) + "%";
+ }
+ Rect rect = new Rect();
+ mPaint.setTextSize(textSize);
+ mPaint.setColor(textColor);
+ mPaint.getTextBounds(percentStr, 0, percentStr.length(), rect);
+ float baseLineHeight = Math.abs(mPaint.getFontMetrics().ascent);
+ canvas.drawText(percentStr + "", centerX - rect.width() / 2, centerY + baseLineHeight / 2, mPaint);
+
+ }
+
+ private void drawNeiYuan(Canvas canvas) {
+ mPaint.setAntiAlias(true);
+ mPaint.setDither(true);
+ mPaint.setColor(neiYuanColor);
+ mPaint.setStyle(Paint.Style.FILL);
+ canvas.drawCircle(centerX, centerY, ringRadius - ringWidth / 2, mPaint);
+ }
+
+ private void drawProgressRing(Canvas canvas) {
+ mPaint.setAntiAlias(true);
+ mPaint.setDither(true);
+ mPaint.setColor(ringProgressColor);
+ mPaint.setStyle(Paint.Style.STROKE);
+ mPaint.setStrokeWidth(ringWidth);
+ mPaint.setShader(null);
+
+ RectF progressRectF = new RectF(centerX - ringRadius, centerY - ringRadius,
+ centerX + ringRadius, centerY + ringRadius);
+ LinearGradient linearGradient = new LinearGradient(progressRectF.left, progressRectF.top,
+ progressRectF.left, progressRectF.bottom,
+ new int[]{
+ Color.parseColor("#33D9FF"),
+ Color.parseColor("#1657D9")
+ }, new float[]{0f, 1f},
+ Shader.TileMode.CLAMP);
+ if (linearGradient != null) {
+ mPaint.setShader(linearGradient);
+ } else {
+ mPaint.setShader(null);
}
- for (int i = 0; i < mListRing.size(); i++) {
- this.mListRing.add(mListRing.get(i));
+ if (isRound) {
+ mPaint.setStrokeCap(Paint.Cap.ROUND);
}
- if (time > 0)
- startAnim(time);
- else
- invalidate();
- }
+ float angle = (float) AndroidUtils.chuFa(viewProgress * getEffectiveDegree(), viewMax, 2);
+ if (!isClockwise) {
+ angle = -1 * angle;
+ }
+ canvas.drawArc(progressRectF, startAngle, angle, false, mPaint);
+ mPaint.reset();//如果不reset,setShader导致设置进度无效果
+ }
+
+ private void drawRing2(Canvas canvas) {
+ mPaint.setAntiAlias(true);
+ mPaint.setDither(true);
+ mPaint.setStyle(Paint.Style.STROKE);
+ mPaint.setStrokeWidth(ringWidth);
+ mPaint.setColor(ringColor);
+ RectF rectF = new RectF(centerX - ringRadius,
+ centerY - ringRadius,
+ centerX + ringRadius,
+ centerY + ringRadius);
+ if (isRound) {
+ mPaint.setStrokeCap(Paint.Cap.ROUND);
+ }
+ float angle = getEffectiveDegree();
- public RingProgress(Context context) {
- this(context, null);
+ if (!isClockwise) {
+ angle = -1 * angle;
+ }
+ canvas.drawArc(rectF, startAngle, angle, false, mPaint);
}
- public RingProgress(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
+ private void drawRing(Canvas canvas) {
+ mPaint.setStyle(Paint.Style.STROKE);
+ mPaint.setStrokeWidth(ringWidth);
+ mPaint.setColor(ringColor);
+ canvas.drawCircle(centerX, centerY, ringRadius, mPaint);
}
- public RingProgress(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(attrs);
+ /*******************************get*set********************************************/
+ public int getEffectiveDegree() {
+ return 360 - disableAngle;
}
-
- private void init(AttributeSet attrs) {
- TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.RingProgress);
- isCorner = typedArray.getBoolean(R.styleable.RingProgress_showRingCorner, false);
- isDrawBg = typedArray.getBoolean(R.styleable.RingProgress_showBackground, false);
- isDrawBgShadow = typedArray.getBoolean(R.styleable.RingProgress_showBackgroundShadow, false);
- rotateAngle = typedArray.getInt(R.styleable.RingProgress_rotate, 270);
- ringWidthScale = typedArray.getFloat(R.styleable.RingProgress_ringWidthScale, 0.5f);
- bgShadowColor = typedArray.getColor(R.styleable.RingProgress_bgShadowColor, bgShadowColor);
- bgColor = typedArray.getColor(R.styleable.RingProgress_bgColor, bgColor);
- SweepAngle = typedArray.getInt(R.styleable.RingProgress_ringSweepAngle, 180);
- typedArray.recycle();
-
-
- initPaint();
+ public float getProgress() {
+ return progress;
}
-
- private void initPaint() {
- mPaint = new Paint();
-
- mPaintText = new Paint();
- mPaintText.setAntiAlias(true);
- mPaintText.setStyle(Paint.Style.FILL);
- mPaintText.setColor(Color.WHITE);
-
+ public void setProgress(float progress) {
+ setProgress(progress, useAnimation);
}
-
- private void setmBitmapBg(Paint paint, Bitmap mBitmapBg) {
- Canvas canvas = null;
- canvas = new Canvas(mBitmapBg);
-
- for (int i = 0; i < mListRing.size(); i++) {
- paint.reset();
- paint.setAntiAlias(true);
- paint.setStrokeWidth(ringWidth);
- paint.setStyle(Paint.Style.STROKE);
- if (isCorner) {
- paint.setStrokeCap(Paint.Cap.ROUND);
- paint.setStrokeJoin(Paint.Join.ROUND);
- }
-
- int red = (bgColor & 0xff0000) >> 16;
- int green = (bgColor & 0x00ff00) >> 8;
- int blue = (bgColor & 0x0000ff);
- int colorvaluer = red + (255 - red) / mListRing.size() * i;
- int colorvalueg = green + (255 - green) / mListRing.size() * i;
- int colorvalueb = blue + (255 - blue) / mListRing.size() * i;
-
- paint.setColor(Color.rgb(colorvaluer, colorvalueg, colorvalueb));
-
-
- Path pathBg = new Path();
- RectF r = new RectF();
- r.top = rectFBg.top + ringWidth * i;
- r.bottom = rectFBg.bottom - ringWidth * i;
- r.left = rectFBg.left + ringWidth * i;
- r.right = rectFBg.right - ringWidth * i;
- mListRing.get(i).setRectFRing(r);
-
- pathBg.addArc(r, 0, SweepAngle);
-
- if (i == 0 && isDrawBgShadow) {
- paint.setShadowLayer(ringWidth / 3,
- 0 - ringWidth / 4,
- 0, bgShadowColor);
- }
- if (isDrawBg)
- canvas.drawPath(pathBg, paint);
-
- }
- bgChange = false;
-
+ public int getDisableAngle() {
+ return disableAngle;
}
-
- private Bitmap getmBitmapBg(Paint paint) {
-
- if (mBitmapBg == null) {
- mBitmapBg = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
- setmBitmapBg(paint, mBitmapBg);
-
+ public void setDisableAngle(int disableAngle) {
+ int beforeDisableAngle = this.disableAngle;
+ if (disableAngle > 360) {
+ this.disableAngle = 360;
+ } else if (disableAngle < 0) {
+ this.disableAngle = 0;
+ } else {
+ this.disableAngle = disableAngle;
}
-
- if (bgChange) {
- mBitmapBg = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
- setmBitmapBg(paint, mBitmapBg);
+ if (useAnimation) {
+ ValueAnimator valueAnimator = ValueAnimator.ofInt(disableAngle, beforeDisableAngle);
+ valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+ @Override
+ public void onAnimationUpdate(ValueAnimator animation) {
+ RingProgress.this.disableAngle = (int) animation.getAnimatedValue();
+ invalidate();
+ }
+ });
+ valueAnimator.setInterpolator(new DecelerateInterpolator());
+ valueAnimator.setDuration(duration);
+ valueAnimator.start();
+ } else {
+ invalidate();
}
-
-
- return mBitmapBg;
-
}
-
- private void drawBg(Canvas canvas, Paint paint) {
- paint.setAntiAlias(true);
- canvas.drawBitmap(getmBitmapBg(paint), 0
- , 0, paint);
+ public int getStartAngle() {
+ return startAngle;
}
+ public RingProgress setStartAngle(int startAngle) {
+ this.startAngle = startAngle;
+ invalidateRingProgress();
+ return this;
+ }
- private void drawProgress(Canvas canvas, Paint paint) {
-
-
- for (int i = 0; i < mListRing.size(); i++) {
-
- paint.reset();
- paint.setAntiAlias(true);
- paint.setStrokeWidth(ringWidth);
- paint.setStyle(Paint.Style.STROKE);
- Path pathProgress = new Path();
- pathProgress.addArc(mListRing.get(i).getRectFRing(), 0,
-
- (int) (SweepAngle / 100f * mListRing.get(i).getProgress() * mAnimatedValue)
-
- );
-
- Shader mShader = new LinearGradient(mListRing.get(i).getRectFRing().left, mListRing.get(i).getRectFRing().top,
- mListRing.get(i).getRectFRing().left, mListRing.get(i).getRectFRing().bottom,
- new int[]{
- mListRing.get(i).getStartColor(),
- mListRing.get(i).getEndColor()
-
- }, new float[]{0f, 1f},
- Shader.TileMode.CLAMP);
-
- paint.setShader(mShader);
- if (isCorner) {
- paint.setStrokeCap(Paint.Cap.ROUND);
- paint.setStrokeJoin(Paint.Join.ROUND);
- }
- canvas.drawPath(pathProgress, paint);
- paint.setShader(null);
-
-
- mPaintText.setTextSize(mPaint.getStrokeWidth() / 2);
-
-
- String textvalue = String.valueOf(mListRing.get(i).getValue());
-
- float arc_length = (float) (Math.PI * mListRing.get(i).getRectFRing().width()
- * (mListRing.get(i).getProgress() / 100f))
- * (SweepAngle / 360f);
-
- float textvalue_length = getFontlength(mPaintText, textvalue);
-
- if (mAnimatedValue == 1) {
-
-
- if (arc_length - textvalue_length * 1.5f <= 0) {
-
-
- float textvalue_length_one = textvalue_length * 1.0f / textvalue.length();
-
-
- int textvalue_size = (int) (arc_length / textvalue_length_one);
-
- if (textvalue_size >= textvalue.length()) {
- canvas.drawTextOnPath(textvalue
- ,
- pathProgress,
- 10,
- getFontHeight(mPaintText) / 3,
- mPaintText);
- } else {
- String text = textvalue.substring(0, 1);
- for (int j = 0; j < textvalue_size; j++) {
- text = text + ".";
- }
-
- canvas.drawTextOnPath(text
- ,
- pathProgress,
- 10,
- getFontHeight(mPaintText) / 3,
- mPaintText);
-
-
- }
- } else {
-
- canvas.drawTextOnPath(textvalue
- ,
- pathProgress,
- (float) (arc_length
- - textvalue_length * 1.5f),
- getFontHeight(mPaintText) / 3,
- mPaintText
-
- );
+ public void setProgress(float progress, boolean useAnimation) {
+ float beforeProgress = viewProgress;
+ if (progress > maxProgress) {
+ this.progress = maxProgress;
+ } else if (progress < 0) {
+ this.progress = 0;
+ } else {
+ this.progress = progress;
+ }
+ viewProgress = progress * viewMax / maxProgress;
+ if (useAnimation) {
+ ValueAnimator valueAnimator = ValueAnimator.ofFloat(beforeProgress, viewProgress);
+ valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+ @Override
+ public void onAnimationUpdate(ValueAnimator animation) {
+ RingProgress.this.viewProgress = (float) animation.getAnimatedValue();
+ RingProgress.this.scaleProgress = RingProgress.this.viewProgress * RingProgress.this.maxProgress / viewMax;
+ invalidate();
+ setRingProgress(RingProgress.this.scaleProgress, RingProgress.this.progress, RingProgress.this.maxProgress);
}
- }
-
-
- String text = String.valueOf(mListRing.get(i).getName());
-
-
- float textlength = getFontlength(mPaintText, text);
-
- float textlength_one = textlength * 1.0f / text.length();
-
- float showtextlength = (float) (arc_length
- - textvalue_length * 1.8f);
- if (showtextlength < 0)
- showtextlength = 0;
-
- float textsize = showtextlength / textlength_one;
-
- if (textsize > text.length()) {
- textsize = text.length();
- } else if (textsize < 1) {
- textsize = 0;
- }
-
- canvas.drawTextOnPath(text.substring(0, (int) textsize)
- ,
- pathProgress,
-
- 10, getFontHeight(mPaintText) / 3,
- mPaintText
-
- );
-
-
+ });
+ valueAnimator.setInterpolator(new DecelerateInterpolator());
+ valueAnimator.setDuration(duration);
+ valueAnimator.start();
+ } else {
+ RingProgress.this.scaleProgress = this.progress;
+ invalidate();
+ setRingProgress(RingProgress.this.scaleProgress, RingProgress.this.progress, RingProgress.this.maxProgress);
}
-
}
-
- @SuppressLint("DrawAllocation")
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
-
-
- canvas.rotate(rotateAngle, getMeasuredWidth() / 2f, getMeasuredHeight() / 2f);
- canvas.save();
-
- if (mListRing.size() > 0)
- ringWidth = (int) (
- mWidth / 2f / (mListRing.size() + 0.5f) * (1 - ringWidthScale));
- mPadding = ringWidth;
- rectFBg = new RectF(getMeasuredWidth() / 2 - mWidth / 2 + mPadding
- , getMeasuredHeight() / 2 - mWidth / 2 + mPadding
- , getMeasuredWidth() / 2 + mWidth / 2 - mPadding
- , getMeasuredHeight() / 2 + mWidth / 2 - mPadding);
-
- drawBg(canvas, mPaint);
- drawProgress(canvas, mPaint);
- canvas.restore();
-
+ public int getNeiYuanColor() {
+ return neiYuanColor;
}
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
- int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
- int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
- int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
- if (widthSpecMode == MeasureSpec.AT_MOST
- && heightSpecMode == MeasureSpec.AT_MOST) {
- setMeasuredDimension(dip2px(30), dip2px(30));
- } else if (widthSpecMode == MeasureSpec.AT_MOST) {
- setMeasuredDimension(heightSpecSize, heightSpecSize);
- } else if (heightSpecMode == MeasureSpec.AT_MOST) {
- setMeasuredDimension(widthSpecSize, widthSpecSize);
- }
- if (getMeasuredWidth() > getHeight())
- mWidth = getMeasuredHeight();
- else
- mWidth = getMeasuredWidth();
+ public RingProgress setNeiYuanColor(@ColorInt int neiYuanColor) {
+ this.neiYuanColor = neiYuanColor;
+ invalidateRingProgress();
+ return this;
}
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- if (w > h)
- mWidth = h;
- else
- mWidth = w;
-
-
+ public int getRingRadius() {
+ return ringRadius;
}
-
- public float getFontlength(Paint paint, String str) {
- return paint.measureText(str);
+ public RingProgress setRingRadius(int ringRadius) {
+ this.ringRadius = ringRadius;
+ invalidateRingProgress();
+ return this;
}
- public float getFontHeight(Paint paint) {
- Paint.FontMetrics fm = paint.getFontMetrics();
- return fm.descent - fm.ascent;
+ public int getRingWidth() {
+ return ringWidth;
}
- public int dip2px(float dpValue) {
- final float scale = getContext().getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
+ public RingProgress setRingWidth(int ringWidth) {
+ this.ringWidth = ringWidth;
+ invalidateRingProgress();
+ return this;
}
-
- public void startAnim(int time) {
- stopAnim();
- startViewAnim(0f, 1f, time);
+ public int getRingColor() {
+ return ringColor;
}
- private ValueAnimator valueAnimator;
- private float mAnimatedValue = 1f;
-
- public void stopAnim() {
- if (valueAnimator != null) {
- clearAnimation();
- valueAnimator.setRepeatCount(0);
- valueAnimator.cancel();
- mAnimatedValue = 0f;
- postInvalidate();
- }
+ public RingProgress setRingColor(@ColorInt int ringColor) {
+ this.ringColor = ringColor;
+ invalidateRingProgress();
+ return this;
}
-
- private ValueAnimator startViewAnim(float startF, final float endF, long time) {
- valueAnimator = ValueAnimator.ofFloat(startF, endF);
- valueAnimator.setDuration(time);
- valueAnimator.setInterpolator(new LinearInterpolator());
- valueAnimator.setRepeatCount(0);
- valueAnimator.setRepeatMode(ValueAnimator.RESTART);
- valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @Override
- public void onAnimationUpdate(ValueAnimator valueAnimator) {
-
- mAnimatedValue = (float) valueAnimator.getAnimatedValue();
- invalidate();
-
-
- }
- });
- valueAnimator.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- super.onAnimationEnd(animation);
-
- }
-
- @Override
- public void onAnimationStart(Animator animation) {
- super.onAnimationStart(animation);
- }
-
- @Override
- public void onAnimationRepeat(Animator animation) {
- super.onAnimationRepeat(animation);
- }
- });
- if (!valueAnimator.isRunning()) {
- valueAnimator.start();
-
- }
-
- return valueAnimator;
+ public int getRingProgressColor() {
+ return ringProgressColor;
}
+ public RingProgress setRingProgressColor(@ColorInt int ringProgressColor) {
+ this.ringProgressColor = ringProgressColor;
+ invalidateRingProgress();
+ return this;
+ }
- float startX = 0;
- float startY = 0;
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
+ /*public int getRingProgressSecondColor() {
+ return ringProgressSecondColor;
+ }
- if (MotionEvent.ACTION_DOWN == event.getAction()) {
- startX = event.getX();
- startY = event.getY();
- return true;
- } else if (MotionEvent.ACTION_UP == event.getAction()) {
- if (Math.abs(event.getX() - startX) < 5 &&
- Math.abs(event.getY() - startY) < 5) {
+ public RingProgress setRingProgressSecondColor(@ColorInt int ringProgressSecondColor) {
+ this.ringProgressSecondColor = ringProgressSecondColor;
+ invalidateRingProgress();
+ return this;
+ }
+*/
+ public boolean isClockwise() {
+ return isClockwise;
+ }
- float distance = (float) Math.sqrt(Math.pow(Math.abs((event.getX() - rectFBg.centerX())), 2)
- + Math.pow(Math.abs((event.getY() - rectFBg.centerY())), 2));
+ public RingProgress setClockwise(boolean clockwise) {
+ isClockwise = clockwise;
+ invalidateRingProgress();
+ return this;
+ }
- for (int i = mListRing.size() - 1; i >= 0; i--) {
+ public Shader getProgressShader() {
+ return progressShader;
+ }
- if (mListRing.get(i).getRectFRing().width() / 2 + ringWidth / 2 > distance
- && mListRing.get(i).getRectFRing().width() / 2 - ringWidth / 2 < distance
- ) {
+ public RingProgress setProgressShader(Shader progressShader) {
+ this.progressShader = progressShader;
+ invalidateRingProgress();
+ return this;
+ }
- float angle =
- Math.round((float)
- (Math.asin(
- Math.abs((event.getY() - rectFBg.centerY())) / distance) / Math.PI * 180));
+ public float getMaxProgress() {
+ return maxProgress;
+ }
- if (event.getY() - rectFBg.centerY() >= 0 &&
- event.getX() - rectFBg.centerX() >= 0) {
- angle = angle + 0;
+ public RingProgress setMaxProgress(float maxProgress) {
+ this.maxProgress = maxProgress;
+ invalidateRingProgress();
+ return this;
+ }
- } else if (event.getY() - rectFBg.centerY() <= 0 &&
- event.getX() - rectFBg.centerX() >= 0) {
- angle = 360 - angle;
- } else if (event.getY() - rectFBg.centerY() <= 0 &&
- event.getX() - rectFBg.centerX() <= 0) {
- angle = 180 + angle;
- } else if (event.getY() - rectFBg.centerY() >= 0 &&
- event.getX() - rectFBg.centerX() <= 0) {
- angle = 180 - angle;
+ public boolean isRound() {
+ return isRound;
+ }
- }
+ public RingProgress setRound(boolean round) {
+ isRound = round;
+ invalidateRingProgress();
+ return this;
+ }
+ public boolean isUseAnimation() {
+ return useAnimation;
+ }
- if (mListRing.get(i).getProgress() / 100f * 360f + rotateAngle <= 360 && rotateAngle <= 360) {
+ public RingProgress setUseAnimation(boolean useAnimation) {
+ this.useAnimation = useAnimation;
+ invalidateRingProgress();
+ return this;
+ }
- if (angle >= rotateAngle && angle <= mListRing.get(i).getProgress() / 100f * 360f + rotateAngle) {
- if (mOnSelectRing != null) {
- mOnSelectRing.Selected(mListRing.get(i));
- }
- }
+ public int getDuration() {
+ return duration;
+ }
+ public RingProgress setDuration(int duration) {
+ this.duration = duration;
+ invalidateRingProgress();
+ return this;
+ }
- } else if (mListRing.get(i).getProgress() / 100f * 360f + rotateAngle >= 360 && rotateAngle <= 360) {
+ public double getProgressPercent() {
+ return progressPercent;
+ }
+ public boolean isDecimal() {
+ return isDecimal;
+ }
- if (angle >= rotateAngle && angle <= 360) {
- if (mOnSelectRing != null) {
- mOnSelectRing.Selected(mListRing.get(i));
- }
- } else if (angle >= 0 && mListRing.get(i).getProgress() / 100f * 360f + rotateAngle - 360 >= angle) {
- if (mOnSelectRing != null) {
- mOnSelectRing.Selected(mListRing.get(i));
- }
- }
+ public RingProgress setDecimal(boolean decimal) {
+ isDecimal = decimal;
+ invalidateRingProgress();
+ return this;
+ }
+ public int getDecimalPointLength() {
+ return decimalPointLength;
+ }
- }
+ public RingProgress setDecimalPointLength(int decimalPointLength) {
+ this.decimalPointLength = decimalPointLength;
+ invalidateRingProgress();
+ return this;
+ }
+ public boolean isShowPercentText() {
+ return isShowPercentText;
+ }
- return true;
+ public RingProgress setShowPercentText(boolean showPercentText) {
+ isShowPercentText = showPercentText;
+ invalidateRingProgress();
+ return this;
+ }
- }
+ public int getTextColor() {
+ return textColor;
+ }
- }
+ public RingProgress setTextColor(@ColorInt int textColor) {
+ this.textColor = textColor;
+ invalidateRingProgress();
+ return this;
+ }
+ public int getTextSize() {
+ return textSize;
+ }
- return true;
+ public RingProgress setTextSize(int textSize) {
+ this.textSize = textSize;
+ invalidateRingProgress();
+ return this;
+ }
- }
+ public OnRingProgressInter getOnRingProgressInter() {
+ return onRingProgressInter;
+ }
+ public RingProgress setOnRingProgressInter(OnRingProgressInter onRingProgressInter) {
+ this.onRingProgressInter = onRingProgressInter;
+ return this;
+ }
+ private void setRingProgress(float scaleProgress, float progress, float max) {
+ if (onRingProgressInter != null) {
+ onRingProgressInter.progress(scaleProgress, progress, max);
}
-
- return false;
}
+ /* public float getViewWidth() {
+ return viewWidth;
+ }
+ public float getViewHeight() {
+ return viewHeight;
+ }*/
+ private void invalidateRingProgress() {
+ invalidate();
+ }
- private OnSelectRing mOnSelectRing;
-
- public OnSelectRing getmOnSelectRing() {
- return mOnSelectRing;
+ private int px2dip(Context context, float pxValue) {
+ float scale = context.getResources().getDisplayMetrics().density;
+ return (int) (pxValue / scale + 0.5F);
}
- public void setOnSelectRing(OnSelectRing mOnSelectRing) {
- this.mOnSelectRing = mOnSelectRing;
+ private int dip2px(Context context, float dipValue) {
+ float scale = context.getResources().getDisplayMetrics().density;
+ return (int) (dipValue * scale + 0.5F);
}
}