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()
Related
Are the x/y positions of a window declared as 1-based? (in other words inclusive of the first pixel of the window), so that with x=1 and y =1 the window would fit in the left top corner without having a 1px gap before its left and top edges?
If that's the case would setting x/y to 0/0 be valid and allowed? Would that cause the window to hang out of the screen area by 1px?
The left top corner corresponds to 0/0 as x/y
UPDATE:
Refer to this question on stackoverflow How do android screen coordinates work?
I have Relative layout with background #mipmap/arrow. I am adding this relative layout (On user touch) in another relative layout which is having ImageView (using imageRelativeLayout.addView(arrowRelativeLayout)).
Goal :
1) I want to add arrow on ImageView after onTouchEvent() to particular (x,y) position. (This is working perfectly)
2) After adding arrow, I want to move arrow's end Point or start Point as per user's touch
3) when user touch on arrow's end point then it should move to respective (x,y) position but start point should be fixed
4)when user touch on arrow's start point then it should move to resp. (x,y) position but end point should be fixed
Arrow Head is Start Point & Arrow tail is End Point
Please Help me
Thanks in advance
So add some invisible views and anchor them to the start end location of the arrows, when a user presses this move them to the new location and then update the image view.
It depends on your graphics if the arrow is edge to edge or corner to corner within the rect, i would suggest corner to corner then you just have to set the layout parameters with the correct top/left and bottom/right also Rotate / Flip it depending on your start end points, but you only need to rotate by 90,180,270 degrees.
This method will mean that your arrow head will be squashed or stretched, if you want you can include a seperate arrow head which uses the angle generated and rotates to this angle, anchoring with the end of the arrow line that you want.
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.
I want to achieve a tilt effect when a button is clicked, on Android OS.
Tilt Effect: Not the whole button will be seen as pressed. Only the part that touch event occured should seem to be pressed.
Is this easily possible on Android?
A simple way would be to use canvas draws to draw 4 sided shapes.
Consider each 4 corners. The "untouched" rectangle would be full size the touched rectangle would be smaller.
You just need to draw your four sided shape using a point you calculate for each part of the rectangle. You can get the touch position, then figure out how much "weight" to give each point.
to calculate each corner, you need to figure out how much "weight" to give the touched coordinate, and how much "weight" to give the untouched coordinate. If you touch the top left corner, that corner would use 100% of the touched coordinate, and the other three corners would all use the untouched coordinate.
If you touched the top middle, you would get a shape like this:
We can calculate the corners for any touch spot, by calculating how far from the corner your touch is
float untouchedXWeight1 = Math.abs(xt - x1)/width;
//maximum of 1, minimum of 0
float untouchedYWeight1 = Math.abs(yt - y1)/height;
float untouchedWeight1 = (untouchedXWeight1 + untouchedYWeight1)/2;
//also maximum of 1, minimum of 0
float touchedWeight1 = 1 - untouchedWeight1;
so with those weights, you can calculate your x and y positions for that corner:
x1 = xUntouched1 * untouchedWeight + xTouched1 * touchedWeight1;
y1 = yUntouched1 * untouchedWeight + yTouched1 * touchedWeight1;
Then do similarly for the other 3 corners.
I've created a first draft here : https://github.com/flavienlaurent/TiltEffect
In a second step, I will make it usable with Button etc.
Unfortunatly, I didn't use the very good (but too mathematical for me) answer of HalR
how to set the screen coordinate system of android screen as first Quadrant of the XY plane
,iwant the (0,0) position to be at bottom left , and i wanna know if i can use the trignometric equation on android screen as Android XY plane is not like the Xy plane
I don't think there's a way to do it that would affect the entire system, such as the XML layout files. But if you just want to draw with a Canvas, you can use translate() and scale().
First use translate() to slide the canvas down so 0,0 is at the bottom. Now the top of the screen would be a negative number, so call scale() to flip it around. Now 0,0 is still at the bottom, and the top of the screen is a positive number.
I'm working with information from this answer and its comments. Use something like:
canvas.save(); // need to restore after drawing
canvas.translate(0, canvas.getHeight()); // reset where 0,0 is located
canvas.scale(1, -1); // invert
... // draw to canvas here
canvas.restore(); // restore to normal
And yes, you can use normal 2D trigonometric functions with the XY coords. You can do it even if they're not translated, you just have to think it through more carefully.
I don't know that you're going to have much luck changing where (0,0) is located, but you could set a constant that accounts for such. myY = y minus screenHeight so (x, myY) adjusts y to the bottom of the screen and works from there +/-.
look up canvas.scale(xs,ys,xp,yp)
xp and yp are the new coordinates that you set for your (0,0) point.