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

时间:2015-05-12 22:16:05

标签: android view layoutparams

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 protected void onFacebookLoginClicked() { Session.openActiveSession(SplashActivity.this, true, permissions, new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { if (session.isOpened()) { Request.newMeRequest(session, new Request.GraphUserCallback() { @Override public void onCompleted(GraphUser user, com.facebook.Response response) { if (user != null) { final Session curSession = Session .getActiveSession(); fbUserid = user.getId(); fbEmail = user.asMap() .get("email") .toString(); fbtoken = curSession .getAccessToken(); fbFirstName = user .getFirstName(); fbLastName = user.getLastName(); fbLink = "https://graph.facebook.com/" + fbUserid + "/picture"; enter code here } } }).executeAsync(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); } class and got the following explanations.

View

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.

Now when we finished with the official documentation, let's see what do we have.

I have an image with original size getLeft() : Left position of this view relative to its parent. 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

500x500

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 <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> and getX() functions, because they actually show me where does the image begin.

As the documentation states, the getY() and getWidth() methods return the width and height of the view but the watch window tells me that my getHeight and getWidth() 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 getHeight and getTop() 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?

3 个答案:

答案 0 :(得分:9)

All these measurement methods return sizes in pixels( template <typename C> class CommonMethods { C * me () { return static_cast<C *>(this); } protected: // ... common data members can go here ... public: void m1 () { //... use me() if needed ... } //... }; class A : public CommonMethods<A>, //... { friend class CommonMethods<A>; //... }; ), not density-pixels ( px ). If you want to convert it you can get the density by calling:

dp

And then divide the values you get with the provided density, for example:

float density = getResources().getDisplayMetrics().density;

答案 1 :(得分:5)

You can get pixels from dp with

--prefix

As of the positioning question. float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ht, getResources().getDisplayMetrics()); float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, wt, getResources().getDisplayMetrics()); and getTop are relative values and are based on your parent. Since the only parent of your getLeft is ImageView you are effectively positioning your LinearLayout directly below the ImageView/ActionBar

Also don't use an image for a circle, you can draw it easily with canvas.drawCircle it takes much less memory.

答案 2 :(得分:5)

这是对未来访客的补充答案。

enter image description here

  • 左上角:当父视图布局子视图时,left是从父视图的左侧到子视图的左侧的距离。同样,top是从父级顶部到子视图顶部的距离。因此,getLeft()getTop()返回相对于其父视图的视图左上角的坐标(而不是屏幕上的绝对坐标)。
  • X,Y:通常,getX()getY()将返回与getLeft()getTop()相同的东西。但是,有时在将视图布置好后将其稍微移动会很有用。可以使用setTranslationX()setTranslationY()来完成。如果已设置,则xy将与lefttop不同,其中

    x = left + translationX
    y = top + translationY
    
  • 宽度,高度:您可以使用getWidth()getHeight()查找视图的宽度和高度。这不受翻译的影响。

以上所有值均为像素尺寸。