自己定义circle


写了一个还不错的自己定义控件。用来展示完毕度或能力值,程序中配置好所占百分比。图中就能够用外面的圆环来显示百分比,效果还是不错的~



代码实现例如以下:

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();
	}
}

以上。


posted @ 2017-05-04 14:52  yfceshi  阅读(294)  评论(0编辑  收藏  举报