Android 自定义View 之 RectF用法详解

112 阅读4分钟
  • 在纸上画矩形

  • @param canvas 纸

*/

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

/**

  • 画矩形 以两个点来画,起点和终点,通常是左上为起点,右下为终点 以下面这个图来看

  • 参数一:起点的Y轴坐标

  • 参数二:起点的X轴坐标

  • 参数三:终点的Y轴坐标

  • 参数四:终点的Y轴坐标

  •  *  
    
  •  *  
    
  •  *  top
    


  • left * * right



  •     bottom  *
    
  •             *
    
  •             *
    
  • 可以看到,左和上无限延长就会在一个点,右和下也是如此,这样应该理解了吧

*/

RectF rectF = new RectF(10,10,200,200);

canvas.drawRect(rectF, customPaint(Color.BLUE));

}

}

运行一下:

在这里插入图片描述

这个看到就是这样的。

然后改一下

paint.setStyle(Paint.Style.STROKE);//空心

在这里插入图片描述

可以看到左边有一部分被遮挡住了

再画长方形

RectF rectF = new RectF(10,10,100,200);//长方形

然后运行

在这里插入图片描述

再多画几个长方形

RectF rectF2 = new RectF(120,10,210,200);//长方形2

canvas.drawRect(rectF2, customPaint(Color.BLUE));

RectF rectF3 = new RectF(240,10,330,200);//长方形3

canvas.drawRect(rectF3, customPaint(Color.BLUE));

RectF rectF4 = new RectF(360,10,450,200);//长方形4

canvas.drawRect(rectF4, customPaint(Color.BLUE));

在这里插入图片描述

再改一下

RectF rectF = new RectF(10,10,100,200);//长方形

canvas.drawRect(rectF, customPaint(Color.GREEN));

RectF rectF2 = new RectF(100,10,190,200);//长方形2

canvas.drawRect(rectF2, customPaint(Color.YELLOW));

RectF rectF3 = new RectF(190,10,280,200);//长方形3

canvas.drawRect(rectF3, customPaint(Color.BLUE));

RectF rectF4 = new RectF(280,10,370,200);//长方形4

canvas.drawRect(rectF4, customPaint(Color.RED));

在这里插入图片描述

再通过改边top的位置,形成从低到高

RectF rectF = new RectF(10,160,100,200);//长方形

canvas.drawRect(rectF, customPaint(Color.GREEN));

RectF rectF2 = new RectF(100,120,190,200);//长方形2

canvas.drawRect(rectF2, customPaint(Color.YELLOW));

RectF rectF3 = new RectF(190,80,280,200);//长方形3

canvas.drawRect(rectF3, customPaint(Color.BLUE));

RectF rectF4 = new RectF(280,40,370,200);//长方形4

canvas.drawRect(rectF4, customPaint(Color.RED));

运行一下

在这里插入图片描述

再整体改一下形成旋转的效果。

RectF rectF = new RectF(10,10,300,100);//长方形

canvas.drawRect(rectF, customPaint(Color.GREEN));

RectF rectF2 = new RectF(300,10,390,300);//长方形2

canvas.drawRect(rectF2, customPaint(Color.YELLOW));

RectF rectF3 = new RectF(100,300,390,390);//长方形3

canvas.drawRect(rectF3, customPaint(Color.BLUE));

RectF rectF4 = new RectF(10,100,100,390);//长方形4

canvas.drawRect(rectF4, customPaint(Color.RED));

最后

都说三年是程序员的一个坎,能否晋升或者提高自己的核心竞争力,这几年就十分关键。

技术发展的这么快,从哪些方面开始学习,才能达到高级工程师水平,最后进阶到Android架构师/技术专家?我总结了这 5大块;

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

Android架构视频+BAT面试专题PDF+学习笔记​》

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望**这份系统化的技术体系**对大家有一个方向参考。

2021年虽然路途坎坷,都在说Android要没落,但是,不要慌,做自己的计划,学自己的习,竞争无处不在,每个行业都是如此。相信自己,没有做不到的,只有想不到的。祝大家2021年万事大吉。