首页 > 代码库 > 自定义circle
自定义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 = http://www.mamicode.com/sweepValue;>
以上。自定义circle
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。