1.
public Yuan(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //圆弧 paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(16f); paint.setColor(Color.BLUE); paint.setStrokeCap(Paint.Cap.ROUND); //进度圆弧 paint1 = new Paint(); paint1.setAntiAlias(true); paint1.setStyle(Paint.Style.STROKE); paint1.setStrokeWidth(16f); paint1.setColor(Color.RED); paint1.setStrokeCap(Paint.Cap.ROUND); //文字 paint2 = new Paint(); paint2.setAntiAlias(true); paint2.setStyle(Paint.Style.FILL); paint2.setStrokeWidth(10f); paint2.setTextSize(20f); paint2.setColor(Color.BLACK); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getMeasuredWidth(); int height = getMeasuredHeight(); rectF = new RectF(0, 0, width, height); setMeasuredDimension(300, 300); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画圆弧 canvas.drawArc(rectF, 0, 360, false, paint); //进度 float sweepAngle = 360 * mCurrent / 100; canvas.drawArc(rectF, 360, sweepAngle, false, paint1); //文字进度 String text = mCurrent + "%"; //文字的宽度 float textWidth = paint2.measureText(text, 0, text.length()); float dx = getWidth() / 2 - textWidth / 2; Paint.FontMetricsInt fontMetricsInt = paint2.getFontMetricsInt(); float dy = (fontMetricsInt.bottom - fontMetricsInt.top); float baseLine = getHeight() / 2 + dy; canvas.drawText(text, dx, baseLine, paint2); if (onLoading != null && mCurrent == 100) { onLoading.complete(); } } private onLoading onLoading; public void setLoading(int mCurrent) { this.mCurrent = mCurrent; invalidate(); } public void setOnLoading(onLoading onLoading) { this.onLoading = onLoading; } public interface onLoading { void complete(); }
2.
yuan = findViewById(R.id.yuan); ValueAnimator animator = ValueAnimator.ofFloat(0, 100); animator.setDuration(6000); animator.setInterpolator(new LinearInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float current = (float) animation.getAnimatedValue(); yuan.setLoading((int) current); } }); animator.start(); yuan.setOnLoading(new Yuan.onLoading() { @Override public void complete() { startActivity(new Intent(TwoActivity.this, MainActivity.class)); } });