这个星期研究了一下,自定义控件。Paint方法,之前看大神的博客,一直有个疑问,RecF()的用法,就是一直没搞懂。今天花了点时间,亲自测试大概已经了解了一些。
查看源码的注释
Rect F holds four float coordinates for a rectangle . The rectangle
is represented by the coordinates of its 4 edges ( left , top , right
bottom ). These fields can be accessed directly . Use width () and
height () to retrieve the rectangle ’ s width and height . Note :
most methods do not check to see that the coordinates are sorted
correctly ( i . e . left <= right and top <= bottom ).矩形F拥有四个浮动坐标矩形。矩形的坐标表示的4边(左,上,右底部)。可以直接访问这些字段。使用宽度()和高度()来检索矩形的宽度和高度。注意:大多数方法不检查坐标是(我正确排序。e。左<
=右和上< =下)。
上面是整个源码的文件注释
以我自己的了解,先看代码
public class LoadingView extends View {
private int mWidth;
private int mHeight;
public LoadingView(Context context) {
this(context, null);
}
public LoadingView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
//当画布的大小为明确值MATCH_CONTENT
mWidth = widthSize;
} else {
mWidth = SizeUtils.dp2px(200);
}
if (heightMode == MeasureSpec.EXACTLY) {
//
mHeight = heightSize;
} else {
mHeight = SizeUtils.dp2px(200);
}
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);//消除锯齿
paint.setStrokeWidth(1);//设置画笔的宽度
paint.setStyle(Paint.Style.STROKE);//设置绘制轮廓
paint.setColor(Color.parseColor("#2EA4F2"));//设置颜色
RectF rectF = new RectF(SizeUtils.dp2px(75), SizeUtils.dp2px(75), SizeUtils.dp2px(125), SizeUtils.dp2px(125));
canvas.drawRect(rectF, paint);
}
}
效果图
根据自己的理解画出来的
我们可以看出有A,B,C,D四个点,分别于RectF(float left, float top, float right, float bottom) 方法对应的是个点。然而中间这个矩形的宽度width=C(right)-A(left),高度height=D(bottom)-B(top)
该效果图,主要是我了好看所以给图形画在画布的中间。矩形的数值可以自行测试。