Android RectF()用法

本文通过实践解析了自定义控件中Paint方法的使用,并重点介绍了RectF类的功能及其实现方式。通过具体代码示例展示了如何创建一个自定义视图,并解释了RectF(float left, float top, float right, float bottom)方法中各参数的意义。

这个星期研究了一下,自定义控件。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)

这里写图片描述

该效果图,主要是我了好看所以给图形画在画布的中间。矩形的数值可以自行测试。

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值