Using MotionEvent - android

From my knowledge x and y starts at the top left of the screen as 0,0
My question is there a calculation to get a positioning in the event? or another way(eg. another new class or method within this class)?
Now the question is vague so I want to add an example:
I remember in math close you have 4 sections in a graph top left, top right, bottom left, and bottom right.
Using the graph style to get variables in:
Getting position from top left to bottom left.
Getting position from bottom right to top right
Getting position from top left to bottom right

Use event.getX() and event.getY() then you can calculate coordinates in any section of graph used in math class.
x, height - y
x, height - y
width - x, height - y

Related

Set min X, max X, min Y, max Y for a Canvas in Android Studio Kotlin

In Android Studio, I created a canvas. Width = 300dp, Height = 300dp.
I draw a line start from left top corner and end to right bottom corner. I traced the x and y coordinates. It is from (0, 0) in left top corner and to (1000+, 1000+) in right bottom corner.
But,
How to set (0,0) in left top corner and (300,300) in right bottom corner?

GridLayout index 0 to start at bottom left

The default GridLayout add elements starting from the top left to bottom right.
Whereby (0,0) is top left. How do I create a GridLayout such that the (0,0) is on bottom left alike to a real life graph coordinate?
I had a bit of a hard time using getChildAt() as the index starts at top left and ends at bottom right.
How do I change it such that it index starts at bottom left and ends at top right?
As my layout is fixed at 15 x 20. I wrote a method such that I can convert easily from a normal x-y coordinate to the index as such
public static int convertToIndex(int x, int y){
int graph_Y = 20 - y;
int index = graph_Y * 15 + x - 1;
return index;
}

Open PopupWindow and drawing from RightTop to LeftBottom, instead of LeftTop to RightBottom

I know I can use:
myPopup.showAtLocation(layout, Gravity.NO_GRAVITY, x, y);
// OR
myPopup.showAtLocation(layout, Gravity.TOP|Gravity.LEFT, x, y);
To open a PopupWindow that's drawn from [x, y] as the Top-Left of the PopupWindow, drawn towards to Bottom-Right.
What I want instead however, is to draw from [x, y] as the Top-Right of the PopupWindow, drawn towards the Bottom-Left.
Here is a picture to make it more clear (The dot is my [x, y] position and the rectangle is my PopupWindow. The first picture shows how it's normally done, and the second is what I want to achieve.):
So how do I correctly calculate the x and y of the second picture's gray point's location, while knowing the black point's location? I know the y can stay the same, but the x should be changed to something like:
x minus PopupWindow-width
The problem is that the PopupWindow's width and height are both set to wrap_content, so I don't know the size until after I draw it.
Does this mean I have to draw it (but make it invisible at first), then calculate the new x with the PopupWindow's MeasuredWidth in it's ViewTreeObserver's OnGlobalLayoutListener (to know when it's done rendering and the MeasuredWidth is known), then apply this new x and then make it Visible? Or is there an easier way to just let it draw at the correct position?
PS: I've also changed Gravity.NO_GRAVITY to Gravity.TOP|Gravity.RIGHT, if the PopupWindow is out of the screen it will automatically place it at the border of the Right/Top side (whichever side it's out of the screen).
You could get the size of your popup window by overriding the onMeasure method of the popup window (note that you have to subclass a View in order to do this). After that, you can calculate the offset of the x and y coordinates. Hope this helps.

Regarding the coordinate of the screen?

I just want to know, isn't the left side coordinate of any UI element is the Y, and the top coordinate is the X, the bottom coordinates is the X and the right coordinates is the Y?
is it the correct coordinate system of the screen, because the (0,0) are placed at the top-left corner of the screen? if so, why oval.set(.., .., .. ..) does not comply to the coorsinates system that starts from the top-left corner (x,y)? in the documentation of, it is states the following:
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
You probably want to use View.getLocationOnScreen() to get your view coordinates
It will return pix offset values relative to screen's TOP-LEFT point.
Note: getLocationOnScreen() cannot be called before layout buildup is completed.
if in doubt is a ViewTreeObserver to be notified when layout is completed.
Note 2: The above offsets will change once screen orientation changes (landscape vs. portrait).
If important to your app's processing you might want to declare a listener for that event.
See function onConfigurationChanged()

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

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);
}
This image will explain you in detail:
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
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
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

Categories

Resources