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

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

更多相关文章

  1. Android使用Drawable实现圆角矩形
  2. Androidの矢量图形之VectorDrawable研究
  3. android 补间动画TranslateAnimation
  4. Android(安卓)动画之补间动画
  5. Android(安卓)自定义底部导航栏和动态添加fragment
  6. Android中动态调整ImageView的宽高比
  7. 【Android】侧滑菜单
  8. android下图片处理方法[转]
  9. Android(安卓)动画效果:四种基础动画的

随机推荐

  1. Google Maps Android(安卓)API V2使用及
  2. 浅谈Android五大布局(一)――LinearLayout
  3. 在Ubuntu16.04上下载并编译Android内核源
  4. Android访问WCF服务(上篇)-服务端开发
  5. Android自学笔记-1-android运行时Dalvik
  6. android中ListView背景设置问题(转)
  7. android 中使用java aes加密算法,报错信息
  8. Android系统input系统(1)
  9. 菜鸟与月薪10万大神的差距都在这 : Andro
  10. Android中系统状态栏的隐藏和显示