寫了一個還不錯的自定義控件,用來展示完成度或能力值,程序中配置好所占百分比,圖中就可以用外面的圓環來顯示百分比,效果還是不錯的~
代碼實現如下:
package com.xys.fastword.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class CircleProgress extends View { private int measureHeigth; private int measureWidth; private Paint mCirclePaint; private float mCircleXY; private float mRadius; private Paint mArcPaint; private RectF mArcRectF; private float mSweepAngle; private float mSweepValue; private Paint mTextPaint; private String mShowText; private float mShowTextSize; public CircleProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CircleProgress(Context context, AttributeSet attrs) { super(context, attrs); } public CircleProgress(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { measureWidth = MeasureSpec.getSize(widthMeasureSpec); measureHeigth = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(measureWidth, measureHeigth); initView(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(mCircleXY, mCircleXY, mRadius, mCirclePaint); canvas.drawArc(mArcRectF, 270, mSweepAngle, false, mArcPaint); canvas.drawText(mShowText, 0, mShowText.length(), mCircleXY, mCircleXY + (mShowTextSize / 4), mTextPaint); canvas.restore(); } private void initView() { float length = 0; if (measureHeigth >= measureWidth) { length = measureWidth; } else { length = measureHeigth; } mCircleXY = length / 2; mRadius = (float) (length * 0.5 / 2); mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setColor(getResources().getColor( android.R.color.holo_blue_bright)); mArcRectF = new RectF((float) (length * 0.1), (float) (length * 0.1), (float) (length * 0.9), (float) (length * 0.9)); mSweepAngle = (mSweepValue / 100f) * 360f; mArcPaint = new Paint(); mArcPaint.setAntiAlias(true); mArcPaint.setColor(getResources().getColor( android.R.color.holo_blue_bright)); mArcPaint.setStrokeWidth((float) (length * 0.1)); mArcPaint.setStyle(Style.STROKE); mShowText = setShowText(); mShowTextSize = setShowTextSize(); mTextPaint = new Paint(); mTextPaint.setTextSize(mShowTextSize); mTextPaint.setTextAlign(Paint.Align.CENTER); } private float setShowTextSize() { this.invalidate(); return 20; } private String setShowText() { this.invalidate(); return "ssssss"; } public void forceInvalidate() { this.invalidate(); } public void setSweepValue(float sweepValue) { mSweepValue = sweepValue; this.invalidate(); } }