最近在看View 的重写,发现很多地方用到了Paint,且自己对这方面欠缺,故作笔记,便于自己不用重复去网上查资料.
画 圆,圆弧,字体,矩形
圆弧就是圆的一小部分,圆有外接正方形,圆弧同样也有外接,矩形所以说如果画圆弧,先画一个矩形,在矩形中内接一段圆弧
public void drawArc(RectF oval, float startAngle, float weepAngle, boolean useCenter, Paint paint)
- oval :指定圆弧的外轮廓矩形区域。
- startAngle: 圆弧起始角度,单位为度。坐标轴,X轴正反向为0°,所以说一般是从270°开始
- sweepAngle: 圆弧扫过的角度,顺时针方向,单位为度,从右中间开始为零度。
- useCenter: 如果为True时,弧的个边界,和圆心连接,为扇形,通常用来绘制扇形。
true![这里写图片描述](https://img-blog.csdn.net/20160705115023308)
false![这里写图片描述](https://img-blog.csdn.net/20160705114906211)
- paint: 绘制圆弧的画板属性,如颜色,是否填充等。
Paint paint = new Paint();
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
mArcPaint.setStrokeWidth((float) (length * 0.1));
设置所用的线宽
先看下效果
代码,是一个自定义的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 CircleProgressView extends View {
private int mMeasureHeigth;
private int mMeasureWidth;
//圆
private Paint mCirclePaint;
private float mCircleXY;
private float mRadius;
//弧
private Paint mArcPaint;
private RectF mArcRectF;
private float mSweepAngle;
private float mSweepValue = 66;
//字
private Paint mTextPaint;
private String mShowText;
private float mShowTextSize;
public CircleProgressView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CircleProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleProgressView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
mMeasureWidth = MeasureSpec.getSize(widthMeasureSpec);
mMeasureHeigth = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mMeasureWidth, mMeasureHeigth);
initView();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制圆
canvas.drawCircle(mCircleXY, mCircleXY, mRadius, mCirclePaint);
// 绘制弧线 坐标轴,X轴正反向为0°,所以说一般是从270°开始
canvas.drawArc(mArcRectF, 270, mSweepAngle, false, mArcPaint);
// 绘制文字
canvas.drawText(mShowText, 0, mShowText.length(),
mCircleXY, mCircleXY + (mShowTextSize / 4), mTextPaint);
}
private void initView() {
float length = 0;
if (mMeasureHeigth >= mMeasureWidth) {
length = mMeasureWidth;
} else {
length = mMeasureHeigth;
}
//圆的位置,
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));
//将一圈360°分为,100份
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 50;
}
private String setShowText() {
this.invalidate();
return "马世豪";
}
public void forceInvalidate() {
this.invalidate();
}
public void setSweepValue(float sweepValue) {
if (sweepValue != 0) {
mSweepValue = sweepValue;
} else {
mSweepValue = 25;
}
this.invalidate();
}
}