自定义View-Rect和RectF_android根据rect坐标添加控件-程序员宅基地

技术标签: RectF  Android 自定义控件  Rect  android  自定义  Android  

Rect 类定义了一个矩形结构,同样实现了 Parcelable 序列化接口。Rect 类定义了 left、top、right、bottom 四个成员变量,我们需要正确理解这 4 个成员变量的作用:
left:矩形左边线条离 y 轴的距离
top:矩形上面线条离 x 轴的距离
right:矩形右边线条离 y 轴的距离
bottom:矩形底部线条离 x 轴的距离

矩形是一种非常常见的图形结构,并且能衍生出更多的图形,如椭圆、扇形、弧线等等;矩形还能进行各种图形运算,如交集、并集等等,所以,与之对应的 Rect 类功能也更加复杂。有人会疑惑为什么不直接指定左上角的坐标、宽度和高度来确定一个矩形,因为指定 top、left、right 和 bottom 更符合坐标系的数学逻辑,也能更好的支持矩形的计算。

Rect 的主要功能有:
1)初始化:主要有两种初始化的方法:一是直接指定 left、top、right、bottom 等 4 个成员变量的值,二是从另一个 Rect 对象中复制。下面是 Rect 的三个构造方法:
  Rect()
  Rect(int left,int top,int right,int bottom)
  Rect(Rect r)

2)增值计算:根据 left、top、right、bottom 等 4 个成员变量计算矩形的宽度、高度或中心点的坐标,主要的方法定义如下:
 public final boolean isEmpty(){
       return left>=right ||top>= bottom;
 }

判断 Rect 是否为空,也就是矩形区域面积是否为 0 或者为无效矩形。


 public final int width(){
      return right - left;
 }

返回矩形的宽度。


 public final int height(){
     return bottom - top;
 }

返回矩形的高度。

public final int centerX(){
     return (left + right) >> 1;
}

计算矩形中心点的 x 坐标,右移一位相当于除以 2,移位运算比普通的除法运算效率

更高。


public final int centerY(){
    return (top + bottom) >> 1;
}

计算矩形中心点的 y 坐标。


public final float exactCenterX(){
   return (left + right) * 0.5f;
}

计算矩形中心点的 x 坐标,返回 float 类型,结果更精确。


 public final float exactCenterY(){
   return (top + bottom) * 0.5f;
 }

计算矩形中心点的 y 坐标,返回 float 类型,结果更精确。


3)改变矩形的位置或大小,通过修改 left、top、right 和 bottom 等 4 个成员变量的值,获取矩形位置平移、放大、缩小等结果。
public void setEmpty(){
  left = right = top = bottom = 0;
}

将矩形的 left、top、right 和 bottom 置 0。


 public void set(int left,int top,int right,int bottom){
  this.left = left;
  this.top = top;
  this.right = right;
  this.bottom = bottom;
}

给 left、top、right 和 bottom 重新赋值。


 public void set(Rect src){
  this.left = src.left;
  this.top = src.top;
  this.right = src.right;
  this.bottom = src.bottom;
}


矩形的 left、top、right 和 bottom 来自于另一个矩形 src。
public void offset(int dx,int dy){
  left += dx;
  top += dy;
  right += dx;
  bottom += dy;
}
矩形的 left 和 right 同时移动相同的距离 dx,矩形的 top 和 bottom 同时移动相同的距

离 dy,实际上就是将矩形移动(dx、dy)距离,正负决定移动的方向。


public void offsetTo(int newLeft,int newTop){
  right += newLeft - left;
  bottom += newTop - top;
  left = newLeft;
  top = newTop;
}

offsetTo()方法也是移位,和 offset()不同的是前者是绝对定位,后者是相对定位。


public void inset(int dx,int dy){
  left += dx;
  top += dy;
  right -= dx;
  bottom -= dy;
}
实现了矩形的缩放功能,缩放中心点就是矩形的中心点,要注意的是 dx、dy 为正数时
表示缩小,负数表示放大。
4)包含测试:支持一个点是否位于矩形内和一个矩形是否位于另一个矩形内。
public boolean contains(int x,int y){
  return left < right && top < bottom
              && x >= left && x < right && y >= top && y < bottom;
}

判断点(x,y)是否位于矩形内。


public boolean contains(int left,int top,int right,int bottom){
  return this.left < this.right && this.top < this.bottom
             && this.left <= left && this.top <= top
             && this.right >= right && this.bottom >= bottom;
}

判断传递过来的矩形是否位于矩形内。


public boolean contains(Rect r){
 return this.left < this.right && this.top < this.bottom
            && left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom;
}
判断传递过来的矩形是否位于矩形内。


矩形的交集与并集运算:交集是指两个矩形相交的公共部分,并集是指两个矩形所占
有最大面积区域。
主要的方法如下:
 public boolean intersect(int left,int top,int right,int bottom){
            if (this.left < right && left < this.right && this.top < bottom && top < this.bottom) {
                if (this.left < left) this.left = left;
                if (this.top < top) this.top = top;
                if (this.right > right) this.right = right;
                if (this.bottom > bottom) this.bottom = bottom;
                return true;
            }
            return false;
 }
传入 Rect 的 left、top、right、bottom,并将构建的 Rect 对象与当前 Rect 对象做交集运算,结果保存在当前 Rect 对象中。

 public boolean intersect(Rect r){
            return intersect(r.left, r.top, r.right, r.bottom);
 }

传入新的 Rect 对象,并将该对象与当前 Rect 对象做交集运算,结果保存在当前 Rect对象中。比如有下面的代码段:

 Rect rect1 = new Rect(0, 0, 400, 400);
 Rect rect2 = new Rect(200, 200, 600, 600);
 rect1.intersect(rect2);

此时,rect1 的 left、top、right、bottom 属性被改变了,分别为 200、200、400、400,
public void union(int left,int top,int right,int bottom){
            if ((left < right) && (top < bottom)) {
                if ((this.left < this.right) && (this.top < this.bottom)) {
                    if (this.left > left) this.left = left;
                    if (this.top > top) this.top = top;
                    if (this.right < right) this.right = right;
                    if (this.bottom < bottom) this.bottom = bottom;
                } else {
                    this.left = left;
                    this.top = top;
                    this.right = right;
                    this.bottom = bottom;
                }
            }
}
public void union(Rect r){
            union(r.left, r.top, r.right, r.bottom);
}
union()方法是计算两个矩形的并集,传入一个新的 Rect,与当前 Rect 进行并集运算,并将结果保存在当前 Rect 对象中。比如有下面的代码段:
 Rect rect1 = new Rect(0, 0, 400, 400);
 Rect rect2 = new Rect(200, 200, 600, 600);
 rect1.union(rect2);
运行后与交集一样,最终的结果保存在 rect1 对象中, rect1 的 left、top、right、bottom属性值分别为:0,0,600,600,也就是说,并集取的是四个方向的最大值。与 Rect 类类似的还有 RectF 类,RectF 类的代码实现与 Rect 如出一辙,主要的不同是 Rect的 left、top、right、bottom 四个成员变量为 int 类型,而 RectF 为 float 类型。在开发中,常常会出现 Rect 与 RectF 相互转换的情况,Rect 类中没有定义与 RectF 相关的任何信息,但在 RectF 类中,则定义了二者相互转换的方法。RectF 转换成 Rect。RectF 定义了两个名为 round 和 roundOut 的方法,round()方法将 RectF类的类型为 float 的 left、top、right、bottom 属性以四舍五入的方式转换成 int 再通过 Rect 类型的参数传回,roundOut()方法虽然和 round()差不多,但在某些情况下返回的矩形区域要大些。


如果还有疑问,扒开源代码探个究竟,我们发现,roundOut()方法中获取 left 和 top 时调用了 FloatMath.floor()方法,该方法返回小于参数的最大值,如 FloatMath.floor(3.5)返回 3;而获取 right 和 bottom 调用了 FloatMath.ceil()方法,该方法返回大于参数的最小值,如
FloatMath.ceil(5.2)返回 6。
 public void round(Rect dst){
            dst.set(FastMath.round(left), FastMath.round(top),
                    FastMath.round(right), FastMath.round(bottom));
 }

public void roundOut(Rect dst){
        dst.set((int) FloatMath.floor(left), (int) FloatMath.floor(top),
                (int) FloatMath.ceil(right), (int) FloatMath.ceil(bottom));
}

Rect 转换成 RectF 就相对简单了,实例化 RectF 时,构造方法支持传递 Rect 对象作为参数:
public RectF(Rect r) {
       if (r == null) {
            left = top = right = bottom = 0.0f;
       } else {
            left = r.left;
            top = r.top;
            right = r.right;
            bottom = r.bottom;
       }
}


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_32113133/article/details/66969990

智能推荐

python3爬虫——requests(一)_明朝那些事爬虫代码-程序员宅基地

文章浏览阅读256次。Requests:让http服务人类虽然Python标准库中的urllib模块包含了平常我们使用的大多数功能,但是它的API使用起来让人感觉不太好,而Request自称”HTTP for Humans“,说明使用更简单方便。Request唯一的非转基因的Python库,人类可以安全享有,Request继承了urllib的所有特性,Request支持HTTP链接保持和连接池,支持使用coo..._明朝那些事爬虫代码

[Jupyter-Notebook] 指定python3路径,配置notebook依赖源_jupyter指定python包的位置-程序员宅基地

文章浏览阅读2.1k次。[Jupyter-Notebook] 指定python3路径,配置notebook依赖源问题 :本机是 mac,通过 brew install jupyter 方式安装的 notebook,发现 notebook 用的版本是 python3.7。在安装 keras,tensorflow的时候,是用 pip3 install keras, tensroflow==1.14.0, "nump..._jupyter指定python包的位置

数据库Mysql+SSM技术开发,SSM 泰州市二手房交易平台系统--03401(上万套实战教程手把手教学,免费领取源码)-程序员宅基地

文章浏览阅读162次。关注●点赞收藏并私信博主,免费领取项目源码哦~总体设计主要包括系统总体结构设计、系统数据结构设计、系统功能设计和系统安全设计等;详细设计主要包括模块实现的关键代码,系统数据库访问和主要功能模块的具体实现等。最后对系统进行功能测试,并对测试结果进行分析总结,及时改进系统中存在的不足,为以后的系统维护提供了方便,也为今后开发类似系统提供了借鉴和帮助。

【部署网站】使用nginx+tomcat部署博客网站_用nignx发布网站和用tomcat部署-程序员宅基地

文章浏览阅读3.1k次,点赞3次,收藏16次。一、什么是静态网站、动态网站?静态网站没有采用任何程序开发,是纯粹使用html语言写出的网站,网页文件名以html或htm结尾。原则上不会受到攻击入侵,但是也无法在网络上实时更新内容,就纯粹的是制作好的页面。动态网站目前的主要开发语言有ASP,JSP,PHP,ASP.NET在制作好之后,都有一个网站管理后台,当以管理员身份登陆时,可以对网站的内容进行增删操作,直接在网上进行这些操作,虽然它可以随时更新,但是速度较慢。并且需要区分的是,动态网站的动态指的是动态实时更新而非网站有动态画面。区分静态网站和动_用nignx发布网站和用tomcat部署

android 实现定时任务,Android 实现定时任务的五种方式的讲解-程序员宅基地

文章浏览阅读3.9k次。1、普通线程sleep的方式,可用于一般的轮询Pollingnew Thread(new Runnable() { @Override public void run() { while (true) { //todo ..._android 定时20个小时

Dr_can模型预测控制笔记与代码实现-程序员宅基地

文章浏览阅读2.7w次,点赞206次,收藏552次。因而我们引入模型预测控制(Model PredictiveControl)的概念,对于一般的离散化系统(因为实际计算机实现的控制系统都是离散的系统,连续系统离散化的方法在此不述)。在k时刻,我们可以测量或估计出系统的当前状态y(k),再通过计算得到的u(k),u(k+1),u(k+2)...u(k+j)得到系统未来状态的估计值y(k+1),y(k+2)...y(k+j);我们将预测估计的部分称为预测区间(Predictive Horizon),将控制估计的部分称为控制区间(Control Horizon)_dr_can

随便推点

zookeeper启动Error: JAVA_HOME is incorrectly set问题解决_error: java_home is incorrectly set: e:\java\jdk1.-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏2次。首先根据错误提示,JAVA_HOME配置错误,找不到java环境。主要原因:在Windows下java默认安装装在C:\Program Files\Java\jdk1.xxx下,路径中存在空格,启动时会找不到正确的路径,在JAVA_HOME上加""即可在zookeeper目录bin下的zkEnv.cmd中新增:set JAVA_HOMEset JAVA_HOME="C:\Program Files_error: java_home is incorrectly set: e:\java\jdk1.8 expected to find java.ex

操作系统概述_多道批处理系统算不算操作系统-程序员宅基地

文章浏览阅读2.2w次,点赞34次,收藏147次。第一章 操作系统概述2021年9月6日 16:401.1 什么是操作系统1.1.1 操作系统的地位和目标1.1.2 操作系统的作用和组成1.1.3 操作系统举例1.2 操作系统的发展历史1.2.1 推动操作系统发展的主要动力1.2.2 手工操作1.2.3 单道批处理系统(simple batch processing)1.2.4 多道批处理系统(multiprogramming system)1.2.5 分时系统(time-sharing system)1.2_多道批处理系统算不算操作系统

数字化转型背景下的金融交易业务中台实践-程序员宅基地

文章浏览阅读140次。引言:目前金融业IT系统大多由业务部门或渠道进行竖井式建设,这种模式的好处是系统专业性强,但同时也给运营及IT管理部门带来分散性阵痛。那么如何在强监管与统一风控的形势下,实现统一管控、快速响应、应需而变、按期交付?中台架构就是在这种背景下应运而生。本文主要以某城商行基于BIIP实施的交易中台的实践案例展开分享,一起和大家探讨企业数字化转型中的背景、技术..._运营转型 业务中台

AWG标准_awg官方规范-程序员宅基地

文章浏览阅读1.5k次。AWG 直径 面积 铜阻抗 (inch) (mm) (kcmil) (mm²) (Ω/km) (Ω/kFT) 0000 (4/0) 0.46 11.684 212 107 0.1608 0.04901 000 (3/0) 0.4096 10.404 168 85 0.202..._awg官方规范

图像修复论文Residual Non-local Attention Networks for Image Restoration阅读笔记-程序员宅基地

文章浏览阅读2.8k次。论文来源:ICLR2019论文链接:pdf (openreview.net)_residual non-local attention networks for image restoration

表达式计算。问题描述:编写程序,计算并输出如下表达式的值:y=其中a,x,y均为float类型,取值为3.1415926。输出结果要求保留小数点后3位。_serialprintln(a)的结果为-程序员宅基地

文章浏览阅读153次。【代码】表达式计算。问题描述:编写程序,计算并输出如下表达式的值:y=其中a,x,y均为float类型,取值为3.1415926。输出结果要求保留小数点后3位。_serialprintln(a)的结果为

推荐文章

热门文章

相关标签