


In-depth understanding of the differences between RectF and Rect in Android development
Rect means "rectangle or rectangle" in Chinese. The Rect object holds four integer coordinate values of a rectangle, and the RectF object holds four float coordinate values of a rectangle. This is the biggest difference between the two. From the perspective of implementation, Rect is a final class that implements the Parcelable interface, and RectF is an ordinary class that implements the Parcelable interface. Except for the different coordinate data types recorded, Rect and RectF provide generally the same methods. .
1. Contact:
are used to represent a rectangular area in the coordinate system, and some simple operations can be performed on it. This rectangular area needs to be represented by two coordinate points, the upper left and the lower right.
2. Difference:
(1). The accuracy is different. Rect uses int type as numerical value, and RectF uses float type as numerical value.
(2). The methods provided by the two types are not exactly the same.
3. Code part
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
RectF and Rect basics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
It is precisely because each rectangular local area contains The four vertex coordinates of left, top, right and bottom, getLeft(), getTop(), getRight() and getBottom() belong to the methods declared by View, so each View subclass or control inherits the above methods, Rect or RectF is similar to one Tool class, encapsulates the calculation relationship of four vertex coordinates. When using getLeft(), getTop(), getRight() and getBottom(), you need to pay attention to two issues:
The first issue: getLeft(), getTop (), getRight() and getBottom() calculate the position relative to its parent container
Second question: getLeft(), getTop(), getRight() and getBottom() calculate the result to 0 because The current View subclass or control has not been drawn. The solution is to calculate when the onClick method is clicked or use the delay calculation of the thread
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
RectF and Rect in-depth
Rect is a final class and does not belong to Inherited, implements the Parcelable interface to perform serialization, and declares four integer attributes in the public scope: left, top, right, and bottom, which are used to record the four vertex coordinates of the View rectangular local area.
1 |
|
1. Create an empty Rect object. The default values of left, top, right and bottom are 0
1 2 3 4 5 6 |
|
2. Create a Rect with specified coordinate values. Object, left, top, right and bottom are the specified values
1 2 3 4 5 6 7 8 9 10 |
|
3. Use the known Rect to create a new Rect object, left, top, right and bottom are the values contained in the known Rect
1 2 3 4 5 6 7 8 |
|
4. Determine whether the current Rect and the specified o are the same, the same conditions: belong to the same object or the left, top, right or bottom attribute values of both are the same
1 2 3 4 5 6 7 8 |
|
5. Calculation The hash code of the Rect attribute value
1 2 3 4 5 6 7 8 |
|
6. Return the four coordinate values of the rectangle in the format of Rect(left,top-right,bottom)
1 2 3 4 5 6 7 |
|
7.With [left,top] The format of [right,bottom] returns the four coordinates of the rectangle, that is, the coordinates of the upper left corner and the lower right corner of the rectangular area
1 2 3 |
|
8. The format of [left,top] [right,bottom] returns the four coordinates of the rectangle. Value, that is, the coordinates of the upper left corner and lower right corner of the rectangular area, the same as the above method
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
9. Return the four coordinate values of the rectangle in the format of left top right bottom, that is, the tiled format, for example: 0 0 400 400 or 100 100 800 300
1 2 3 4 5 6 7 8 9 10 |
|
10. Given a string in flat format, such as: 0 0 400 400, determine whether it is legal, and then convert it to a Rect object
1 2 3 4 5 |
|
11 . Write the attribute values contained in Rect into the given PrintWriter stream in the format of [left, top] [right, bottom]
1 2 3 |
|
12. Determine whether Rect is an empty object, that is, whether the included attribute values are Not 0
1 2 3 |
|
13. Calculate the width of the rectangular area
1 2 3 |
|
14. Calculate the height of the rectangular area
1 2 3 |
|
15. Calculate the horizontal center point of the rectangular area and calculate the result If it is a fraction, the nearest integer is returned, for example: the horizontal center point 400
1 2 3 |
|
16. Calculate the vertical center point of the rectangular area. If the calculation result is a fraction, the nearest integer is returned, for example: vertical Center point 850
1 2 3 |
|
17. Calculate the horizontal center point of the rectangular area and return the result float type, for example: horizontal center point 400.0
1 2 3 |
|
18. Calculate the vertical center point of the rectangular area and return the result float type, for example: vertical center point 850.0
1 2 3 |
|
19. Set the attribute value contained in the Rect object to 0
1 2 3 4 5 6 |
|
20. Set the attribute value of Rect to the specified value
1 2 3 4 5 6 |
|
21. Copy the attribute value contained in the specified Rect object
1 2 3 4 5 6 |
|
22. Increase the dx and dy distances in the horizontal and vertical directions of the current rectangular area, that is, expand
1 2 3 4 5 6 |
|
23. Offset dx and dy distances in the horizontal and vertical directions of the current rectangular area, that is, horizontal translation dx and vertical translation dy
1 2 3 4 5 6 |
|
24. Reduce dx in the horizontal and vertical directions of the current rectangular area respectively. , dy distance, that is, reducing
1 2 3 4 |
|
25. Calculate whether the specified coordinates (x, y) are included in the rectangular area, return true if included, otherwise return false
1 2 3 4 5 6 7 |
|
26. Calculate the specified Whether the left, top, right, and bottom vertices are included in the rectangular area, return true if included, otherwise return false
1 2 3 4 5 6 |
|
27. Calculate whether the specified Rect is included in the rectangular area, return true if included, otherwise Return false
1 2 3 4 5 6 7 8 9 10 |
|
28. Calculate whether there is an intersection area between the current Rect and the specified left, top, right, and bottom vertices. Return true and return the specified coordinates, otherwise return false
1 2 3 |
|
29、计算当前Rect与指定的Rect是否存在交集区域,存在返回true并且返回指定坐标,否则返回false
1 2 3 4 5 6 7 8 9 10 |
|
30、计算指定的a、b是否存在交集区域,存在返回true并且返回最大坐标,否则返回false
1 2 3 |
|
31、计算当前Rect与指定的left、top、right、bottom顶点是否存在交集区域,存在返回true并且不返回指定坐标,否则返回false
1 2 3 |
|
32、计算指定的a、b是否存在交集区域,存在返回true并且不返回最大坐标,否则返回false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
33、计算当前Rect与指定的left、top、right、bottom顶点是否存在并集区域,存在更新当前矩形区域,否则不更新
1 2 3 |
|
34、计算当前Rect与指定的Rect是否存在并集区域,存在更新当前矩形区域,否则不更新
1 2 3 4 5 6 7 8 9 10 11 12 |
|
35、计算当前Rect与指定的坐标(x,y)是否存在并集区域,存在更新当前矩形区域,否则不更新
1 2 3 4 5 6 7 8 9 10 11 12 |
|
36、排序当前矩形区域,符合:left
1 2 3 4 5 6 7 8 |
|
37、按照指定的值缩放当前矩形区域
1 2 3 4 5 6 7 8 |
|
38、按照指定的值缩放当前矩形区域

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...
