自定义圆环

今天公司用到一个这东西就写了一个,
/**
* Created by T410-2 on 2018/1/18.
* 我们选取的位置都是已0,0点开始,
* 然后来画图,最后画的图的位置是由我们布局,
* 或者自身在把他移动到任何位置上;
*/

public class BroudProgress extends View {
private Paint mPaint= new Paint();
private int mRoundColor;
private int mRoundPross;
private int mRoundSize;
private int mTextColor;
private int mTextsize;
//画字体
private int mMax= 100;
private int mProgress= 50;

public BroudProgress(Context context) {
this(context, null);
}

public BroudProgress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public BroudProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable. BroudProgress);
mRoundColor = mTypedArray.getColor(R.styleable. BroudProgress_roundcolor, Color. RED);
mRoundPross = mTypedArray.getColor(R.styleable. BroudProgress_roundProssColor, Color. BLUE);
mRoundSize=mTypedArray.getDimensionPixelSize(R.styleable. BroudProgress_roundSize, 5);
mTextColor = mTypedArray.getColor(R.styleable. BroudProgress_textColor, Color. BLUE);
mTextsize= mTypedArray.getDimensionPixelSize(R.styleable. BroudProgress_textsize, 15);
mTypedArray.recycle();

}

/**
* getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小。
* getWidth(相对于屏幕来说)获取的是这个view最终显示的大小,
* 这个大小有可能等于原始的大小也有可能不等于原始大小
*
* 第二说法:getMeasuredHeight()与getHeight()的区别:当屏幕可以包裹内容的时候,
* 他们的值相等当view的高度超出屏幕时,getMeasuredHeight()是实际View的大小,
* 与屏幕无关,getHeight的大小此时则是屏幕的大小。此时,getMeasuredHeight() = getHeight+超出部分。
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor( mRoundColor);
mPaint.setStrokeWidth( mRoundSize);
mPaint.setStyle(Paint.Style. STROKE);
float with=getWidth()/ 2;
float radius=with- mRoundSize/ 2;
canvas.drawCircle(with,with,radius, mPaint);
//画字体
float measureText = mPaint.measureText( mProgress + "%"); //测量字体的大小
//center - textWidth / 2, center + textSize / 2
float cxtext=with-measureText;
float cytext=with+ mTextsize/ 2;
mPaint.setStrokeWidth( 0);
mPaint.setColor( mTextColor);
mPaint.setTextSize( mTextsize);
canvas.drawText( mProgress + "%",cxtext,cytext, mPaint);
//画弧线
RectF mRectF= new RectF(with-radius,with-radius,with+radius,with+radius);
mPaint.setStrokeWidth( mRoundSize);
mPaint.setStyle(Paint.Style. STROKE);
mPaint.setColor( mRoundPross);
canvas.drawArc(mRectF, 0, 360* mProgress/ 100, false, mPaint);


}

/**
* 设置进度
* @param progress
*/
public void setProgress( int progress){
this. mProgress=progress;
if (progress> 100){
progress= 100;
}
postInvalidate();
}
}
收藏
分享到:
举报
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值