自定义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

智能推荐

【数字信号处理】关于实现FIR滤波器的一些问题_信号与fir滤波器长度不同-程序员宅基地

文章浏览阅读2.8k次。在知乎上看到了一个提问,稍微整理了一下关于这些问题的回答https://www.zhihu.com/question/29945169FIR其实就是下面这个公式,其中数组x[]为输入,数组h[]为滤波参数(已知),数组y[]为滤波输出:问题1“实现FIR滤波过程就是上面这个过程?这么简单?还是自己理解错了根本不是这么回事?(想确认:对于实现这块,上面的理解对不对)”从理论上,FIR就是上面这个过程,但是做成实际使用的系统会遇到各种问题阻止你实现这个公式。情况1:已知FIR滤波器在时域的序列这可_信号与fir滤波器长度不同

用JavaScript实现选项卡功能,当选择全选按钮的时候,下边的复选框所以按钮被选中,当取消一个复选框按钮时,全选框按钮失去效果,当再次把所有复选框按钮选择实现全选_、实现操作复选框,要求是可以选择部分选项,也可以全选,全选按钮的状态根据选中的选项个数自动变化,即全-程序员宅基地

文章浏览阅读779次。点击全选,让下边的按钮都被选中,以及复选框的交互简单上代码啦~~CSS样式<style> * { padding: 0; margin: 0; } .wrap { width: 300px; margin: 100px auto 0; } table { border-collapse: _、实现操作复选框,要求是可以选择部分选项,也可以全选,全选按钮的状态根据选中的选项个数自动变化,即全选按钮的状态回自动取消或者自动勾选。

http请求工具类HttpClientUtil(get使用body,post乱码问题解决)_((httpentityenclosingrequestbase) httppost.setenti-程序员宅基地

文章浏览阅读3.4k次。最近很多发送http请求的需求存在,书写下util1:配置需要的依赖在pom.xml中配置http相关依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <s..._((httpentityenclosingrequestbase) httppost.setentity(new inputstreamentity(i

【原创】超全自用idea常用插件记录_idea feign插件-程序员宅基地

文章浏览阅读1.7k次。注:idea插件可以使用账号同步,建议使用账号同步进行设置,这里作为使用记录_idea feign插件

回文日期_输出回文日期-程序员宅基地

文章浏览阅读3.1k次。链接:https://ac.nowcoder.com/acm/contest/216/A来源:牛客网 题目描述众所周知,小K是nowcoder的暴政苟管理,所以小K很擅长踢树,虽然本题与踢树无关小K喜欢将日期排列成yyyy-mm-dd的形式(位数不足添零补齐)的形式,虽然这与小K只会做回文字符串这道水题无关,但小K觉得日期组成的回文串也是挺可爱的。作为一个凉心出题人,小K决定给你一..._输出回文日期

深入了解C语言(函数的参数传递和函数使用参数的方法) _c语言中prog03_06了解函数-程序员宅基地

文章浏览阅读1k次。 深入了解C语言(函数的参数传递和函数使用参数的方法) 深入了解C语言(函数的参数传递和函数使用参数的方法)tangl_99(原作) C语言生成的代码在执行效率上比其它高级语言都高.现在让我们来看看C语言生成的代码具体是什么样子的.当你看完本文对于C语言的了解一定会更深一步了. 本文通过一个个实际案例程序来讲解C语言. 研究案例一 工具: Turboc C v2.0,Debug_c语言中prog03_06了解函数

随便推点

python文件操作(open()、write()、writelines()、read()、readline()、readlines()、seek()、os)_python open writeline-程序员宅基地

文章浏览阅读3.7k次,点赞4次,收藏23次。python文件操作(open()、write()、writelines()、read()、readline()、readlines()、seek()、os)_python open writeline

分布式限流实战--redis实现令牌桶限流_分布式令牌限流-程序员宅基地

文章浏览阅读9.5k次,点赞4次,收藏43次。这篇文章我们主要是分析一下分布式限流的玩法。 因为限流也是一个经典用法了。1.微服务限流随着微服务的流行,服务和服务之间的稳定性变得越来越重要。缓存、降级和限流是保护微服务系统运行稳定性的三大利器。缓存的目的是提升系统访问速度和增大系统能处理的容量,而降级是当服务出问题或者影响到核心流程的性能则需要暂时屏蔽掉,待高峰或者问题解决后再打开,而有些场景并不能用缓存和降级来解决,比如稀缺资源、数据库的写操作、频繁的复杂查询,因此需有一种手段来限制这些场景的请求量,即限流。比如当我们设计了一个函数,准备上线_分布式令牌限流

【Linux】文件系统-程序员宅基地

文章浏览阅读1.7k次,点赞27次,收藏22次。了解磁盘的物理结构、磁盘的具体物理结构、逻辑抽象、软硬连接,动静态库

python实现ks算法_python, 在信用评级中,计算KS statistic值-程序员宅基地

文章浏览阅读456次。# -*- coding: utf-8 -*-import pandas as pdfrom sklearn.grid_search import GridSearchCVfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.u..._ks_statistic

类加载过程 与 代码的执行顺序_类加载后代码的执行顺序-程序员宅基地

文章浏览阅读5k次。https://www.cnblogs.com/ysocean/p/8194428.html 代码的执行顺序https://www.jianshu.com/p/3556a6cca7e5类加载过程_类加载后代码的执行顺序

Oracle LiveLabs实验:Introduction to Oracle Spatial Studio_oracle_spatial 可视化-程序员宅基地

文章浏览阅读601次。本实验介绍了适用于 Oracle Spatial Studio。他既可以在云上,也可以在本地作为Java应用部署。介绍详见这里。此实验申请地址在这里,时间为120分钟。此实验的帮助见这里。本实验使用的地图为OpenStreetMap,即免费的维基世界地图。此实验会自动创建一个ADW,需要通过OCI Console完成初始化配置,然后可以通过网页访问Spatial Studio简介在本次研讨会中,您将探索 Spatial Studio 用于自助式空间分析和可视化的功能。 使用交通事故、警察局和警察_oracle_spatial 可视化

推荐文章

热门文章

相关标签