频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
自定义circle
2014-10-09 11:01:41           
收藏   我要投稿


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


\

代码实现如下:

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

以上。

点击复制链接 与好友分享!回本站首页
相关TAG标签
上一篇:[leetcode]Distinct Subsequences @ Python
下一篇:TM1629操作源代码-LED驱动IC
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站