I am writing a drag and drop application and got really confused because of some parameters.
Please help to figure out.
First of all, I read the documentation for the View
class and got the following explanations.
getX() : The visual x position of this view, in pixels.
getY() : The visual y position of this view, in pixels.
getWidth() : Return the width of the your view.
getHeight() : Return the width of the your view.
getTop() : Top position of this view relative to its parent.
getLeft() : Left position of this view relative to its parent.
Now when we finished with the official documentation, let's see what do we have.
I have an image with original size 500x500
called circle.
And here's the actual screenshot of my application
Here is the xml for the layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/circle" />
</LinearLayout>
Now what am I concerned about. When I watch my locals, I get the following, which really confuses me.
I don't see any problem with the getX()
and getY()
functions, because they actually show me where does the image begin.
As the documentation states, the getWidth()
and getHeight
methods return the width and height of the view but the watch window tells me that my getWidth()
and getHeight
are 300, which I really can't understand, because in my XML I've set them 100dp each, so do the functions return me them in a different measurement, and how do I convert it to dp.
And finally, it tells me that getTop()
and getLeft
are 700 and 300, and as the documentation says, they are the position of the image relative to it's parent. But isn't my parent the Linear Layout, so what do this numbers mean in sense of screen positioning?
This is a supplemental answer for future visitors.
left
is the distance from the left side of the parent to the left side of the subview. Likewise, top
is the distance from the top of the parent to the top of the subview. Thus, getLeft()
and getTop()
return the coordinates of the top left corner of the view relative to its parent view (not the absolute coordinates on the screen).X, Y: Usually getX()
and getY()
will return the same thing as getLeft()
and getTop()
. However, sometimes it is useful to move the view a little after it has already been laid out. This can be done with setTranslationX()
and setTranslationY()
. If these have been set then x
and y
will be different from left
and top
, where
x = left + translationX
y = top + translationY
Width, Height: You can find the width and the height of the view with getWidth()
and getHeight()
. This is not affected by a translation.
The above values are all in pixel dimensions.
All these measurement methods return sizes in pixels( px
), not density-pixels ( dp
). If you want to convert it you can get the density by calling:
float density = getResources().getDisplayMetrics().density;
And then divide the values you get with the provided density, for example:
int widthDp = (int)(img.getWidth() / density);
You can get pixels from dp with
float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ht, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, wt, getResources().getDisplayMetrics());
As of the positioning question.
getTop
and getLeft
are relative values and are based on your parent. Since the only parent of your ImageView
is LinearLayout
you are effectively positioning your ImageView
directly below the ActionBar
/ToolBar
Also don't use an image for a circle, you can draw it easily with canvas.drawCircle it takes much less memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With