Android view's getTop(), getLeft(), getX(), getY(), getWidth(), getHeight() methods

十年热恋 提交于 2021-02-07 05:11:44

问题


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.

enter image description here

And here's the actual screenshot of my application

enter image description here

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.

enter image description here

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?


回答1:


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



回答2:


This is a supplemental answer for future visitors.

enter image description here

  • Left, Top: When a parent view lays out a subview, 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.




回答3:


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.



来源:https://stackoverflow.com/questions/30202379/android-views-gettop-getleft-getx-gety-getwidth-getheight-meth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!