What does top, left, right and bottom mean in Android Rect object

AndroidAndroid CanvasAndroid ViewAndroid Shape

Android Problem Overview


I have an Android project where I should make Apples fall. The apples are painted in a Rect. So I created a function that change the Rect position and repaint. Here's my function :

private void updateApplesPosition() {
    for(Rect rect:fallingDownFruitsList)
        rect.set(rect.left, rect.top +10, rect.right, rect.bottom +10);
}

I have a problem : the Apples don't fall but go from right to left. To make the apples fall I changed the code by this :

private void updateApplesPosition() {
    for(Rect rect:fallingDownFruitsList)
        rect.set(rect.left+10, rect.top, rect.right+10, rect.bottom);
}

Android Solutions


Solution 1 - Android

This image will explain you in detail:

enter image description here

> left The X coordinate of the left side of the rectangle > > top The Y coordinate of the top of the rectangle > > right The X coordinate of the right side of the rectangle > > bottom The Y coordinate of the bottom of the rectangle

enter image description here

Solution 2 - Android

From the docs

Parameters

> left The X coordinate of the left side of the rectangle > > top The Y coordinate of the top of the rectangle > > right The X coordinate of the right side of the rectangle > > bottom The Y coordinate of the bottom of the rectangle

Solution 3 - Android

Adding a crucial information.

The documentation says:

> Note that the right and bottom coordinates are exclusive.

So if your rectangle is a single pixel at position 10,10

left = 10 : The X coordinate of the left side of the rectangle

top = 10 : The Y coordinate of the top of the rectangle = 10

right = 11 : The X coordinate of the right side of the rectangle plus one

bottom = 11 : The Y coordinate of the bottom of the rectangle plus one

> Note that the right and bottom coordinates are exclusive.

The methods getWidth is declared as such

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

Here it will return 11 - 10 = 1, as expected.

https://developer.android.com/reference/android/graphics/Rect

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHunsuView Question on Stackoverflow
Solution 1 - AndroidLOG_TAGView Answer on Stackoverflow
Solution 2 - AndroidadnealView Answer on Stackoverflow
Solution 3 - AndroidPascal GanayeView Answer on Stackoverflow