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 fo
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.
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);
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.