程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 自定義circle

自定義circle

編輯:C++入門知識

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

以上。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved