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;
}
Related
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
Why is it that it is needed to specify absolute x and y co-ordinate values for pop up window object to be shown as drop down? Why does it eventhough not automatically show nearby of the control item that has been clicked enough, only for a one single time all though, once upon a time?
Thank you in advance.
From the android docs:
without cords
void showAsDropDown(View anchor) Display the content view in a popup
window anchored to the bottom-left corner of the anchor view.
with cords:
void showAsDropDown(View anchor, int xoff, int yoff) Display the
content view in a popup window anchored to the bottom-left corner of
the anchor view offset by the specified x and y coordinates.
From these 2 methods we can learn that there are 2 (actually 3) showAsDropDown methods.
The one without cords will do what you want automatically in the bottom-left corner. And the one with cords will just take some offset from the bottom-left corner. the offset will be determined by you (int xoff, int yoff), xoff stands for x offset and yoff stands for y offset.
Source
To get view right edge after rotating it around Y-axis with fixed pivot point I'm using the below code and that is working like it should.
view.setPivotX(0);
view.setPivotY(view.getHeight() / 2);
view.animate().rotationY(rotationAngle).start();
view.getMatrix().mapRect(rectF);
int rightEdge = rectF.right;
Now what I want to do next is to calculate rotationAngle so the right edge of the view will be a specified value (let say 1000px). Any idea how to reach this ?
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
I have an image view which is contained within a relative layout. I am trying to get the screen y coordinates of the top of the image view and the screen y coordinates of the bottom of the image view. I have tried this:
float yMin = (float)levelH.getTop();
float yMax = (float)levelH.getBottom();
float yMin seems almost correct. I am translating another image view (IM2) up and down this image view(IM1). So I am trying to set a limit on how far (IM2) can translate up and down. So my thinking was to get the y top and bottom of (IM1) I can set those as max and min.
Anyone know how to do this?
ps Im using android accelometer to move (IM2)
getTop() ansd getBottom() look at coordinates within it's parent. To get coordinates of it's position on the screen you can use getLocationOnScreen
Use it like this:
int[] coords = {0,0};
view.getLocationOnScreen(coords);
int absoluteTop = coords[1];
int absoluteBottom = coords[1] + view.getHeight();
Use View.getLocationOnScreen() and/or getLocationInWindow()
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