How to get coordinates of a view in constraint layout? - android

I have an ImageView in the center of a constraint layout. I want to get its coordinates in the screen for animation. I have referred similar questions, but I always get 0 as the coordinate.
How can I get the coordinates?
What I have tried:
imageView.getTop();
imageView.getX();
imageView.getPivotX();
Rect rect = new Rect();
imageView.getGlobalVisibleRect(rect);
rect.centerX()
Note:
ImageView is placed at the center using constraints and it is of dimension 40dp x 40dp (small image).

Related

Relation between canvas coordinates and screen coordinates android

What is the relation between screen's X and Y and canvas's X and Y. Suppose canvas's height is half of screen's height and it's center aligned. Now, i want to draw something with respect to screen's coordinates. What should I do? Please help
When you draw in your canva in your onDraw method it is relative to your view. You can't draw beyond your view. For exemple if your view is at the position x = 0, y = 0, width = 50, height = 50. If you try to draw a line in your view like that canva.drawline(25,0,100,0) it's just gonna draw a line from 25x to 50x in your view, it's not gonna go beyond the limit of the View.
Tell me if you need clarification ;).
Edit:
So taking your exemple:
If your view is at the position 200dp, 100dp, 50w, 50h. Let's say you want to draw a line from the 0x,0y axes of your screen to the edge 0x,0y axes of your view. For this you have to do a custom that will match_parent in width and height. Then you will have to have the x, y of your view (view.getX(), view.getY()). The in the onDraw you'll just have to do that:
canvas.drawLine(0,0, myview.getX(), myview.getY(), paint);
You can have something like that:

Android Bounding Rectangles

I'm trying to make a bounding rectangle around an ImageView for collision detection. Unfortunately, I can only find code for sprites and not ImageViews. Is there a way to make a rectangle that goes outside of an image for an ImageView? One of the ImageViews moves through animation while the other one stays still.
Using imageViews for move animation isn't best idea. It takes too many resources. However you can set width and height of imageView to WRAP_CONTENT and use sizes of ImageView as bounding rect
In your xml layout set android:layout_height and android:layout_width of your ImageView to "wrap_content". And when you need bounding rect of ImageView:
...
int[] l = new int[2];
imageView.getLocationOnScreen(l);
int x = l[0];
int y = l[1];
int w = imageView.getWidth();
int h = imageView.getHeight();
...
x, y, w ,h — required bounding rect in screen coordinates

What is programmatically equivalent to android:layout_centerInParent="true" of RelativeLayout?

I place an ImageView of a pin in the center of the layout by using android:layout_centerInParent="true" in my RelativaLayout XML file.
Now I wish to draw the green dot at the same position as the pin on the canvas.
NOTE: the green dot is NOT a view. It is drawn on canvas by canvas.drawCircle();
That is, I have to programmatically get the coordinates of the pin.
So how can I get the coordinates of android:layout_centerInParent="true" with codes?
My guess is
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
You can get layoutParams by relativeLayout.getLayoutParams(), and don't forget to setLayoutParams back when you're done modifying it.
To get the width and height of the parent you can do this.
RelativeLayout parent = (RelativeLayout) findViewById(R.id.yourRelativeLayout);
int width = parent.getWidth();
int height = parent.getHeight();
Then you can divide these numbers by 2 and set that to as your green dot's coordinates and it should appear in the middle of your screen. For this to work your canvas size has to be the same as the relative layout.
But beware, you need to call getWidth() and getHeight() methods after the activity has been created, else you will end up getting zero. See this answer
So, the canvas you are drawing to is in a view that is contained by the RelativeLayout, but you want to draw the dot at the center of the RelativeLayout?
Yes, that is exactly what I am trying to do!
Assuming the canvas view is a direct child of the RealtiveLayout, this should work.
You can get the layout's center by using getWidth() / 2 and getHeight() / 2 on the layout as others mentioned. However, you also have to figure out where the origin of the canvas is. For this you can just use getLeft() and getTop() on the canvas view. Then you just subtract the center x from left, and center y from top to get your final spot.
Example:
Assume each grid line is 1. The RelativeLayout is the large black rectangle, and the Canvas view is the blue one. The center dot's coordinates are 4,6. Using left/top, you get 1,4 for the canvas origin(red dot). Subtract, and you get 3,2, which are the local canvas coordinates for the green dot.
Drawing the dot in the center of the canvas should just be a matter of dividing the width and height of the view by two. If you want it to be at the base of the pin (as opposed to the true center), then just add half the height of the pin to the circle's y value.

Set Text at center of layout that contains Canvas

I have a situation like below
In my linear layout i added one View (MyView) that contains canvas
drawLayout.addView(new MyView(this,"a"));
Now i want to draw one text in canvas at middle of the linearlayout, for that i calculate linearlayout's height and width (drawWidth,drawHeight) and then wrote this,
canvas.drawText(letterTOdraw,drawWidth/2,drawHeight/2,mpaint);
But it was not draw correctly (not in center). After that i modify it as below
canvas.drawText(letterTOdraw,canvas.getWidth()/2,canvas.getHeight()/2,mpaint);
But nothing changed. When i calculate canvas height and width, i am surprised that i gave me 600 X 1024 , how it is possible that i set MyView into linearLayout that is only 951X359.
Thus i can't able to draw text at center of linearLayout.
I am stuck in this problem for last 5 hrs . help me to get out of this.
Your centering code is wrong. Your code will draw the text not in the center, but rather slightly to the bottom and to the right - the center of the canvas will be the top-left point of the text.
If you want to draw the text in the center, you need to take into account the size of the text - at ((canvas.getWidth() - letterWidth) /2, (canvas.getHeight() - letterHeight) / 2))

Is there a way to find the on-screen coordinates of an ImageView's drawable?

I have an activity which consists of just an ImageView. The drawable contained in the ImageView is a Bitmap that was captured from the camera. The image is automatically scaled (maintaining the Bitmap's aspect ratio) so that the scaled width matches the width of the ImageView, and the image is centered on the screen vertically.
I need to figure out the coordinates of the top-left pixel of the actual drawable (not of the ImageView itself), but the ImageView class doesn't seem to give me a way of doing that.
I feel like I could potentially calculate it based on the dimensions of the original bitmap and the dimensions of the ImageView, but that would involve a lot of math that should be unnecessary, and would be prone to floating point errors.
Does anyone know of a way to find the coordinates of the top-left pixel of the Drawable relative to the ImageView?
Thanks.
Looking at the ImageView source code, it translates a Matrix halfway the vertical size (i.e., center it vertically), so you could retrieve it with the ImageView getImageMatrix() method and check the vertical translation by Matrix#getValues(float[]). If I read it right, it will be the sixth value.
I can't test it right now, but it would be something like:
Matrix matrix = iv.getImageMatrix ();
float[] values = new float[];
matrix.getValues(values);
float startY = values[5];

Categories

Resources