Android视图的getTop(),getLeft(),getX(),getY(),getWidth(),getHeight()方法 [英] Android view's getTop(), getLeft(), getX(), getY(), getWidth(), getHeight() methods

查看:157
本文介绍了Android视图的getTop(),getLeft(),getX(),getY(),getWidth(),getHeight()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个拖放应用程序,由于某些参数,我感到非常困惑.

I am writing a drag and drop application and got really confused because of some parameters.

请帮忙弄清楚.

首先,我阅读了 View 类的文档,并获得了以下说明.

First of all, I read the documentation for the View class and got the following explanations.

getX():此视图的可视x位置,以像素为单位.

getY():此视图的视觉y位置,以像素为单位.

getWidth():返回视图的宽度.

getHeight():返回视图的宽度.

getTop():此视图相对于其父视图的顶部位置.

getLeft():此视图相对于其父视图的左侧位置.

现在,当我们完成正式文档后,让我们看看我们有什么.

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

我有一张原始尺寸为 500x500 的图片,称为圆.

I have an image with original size 500x500 called circle.

这是我的应用程序的实际屏幕截图

And here's the actual screenshot of my application

这是布局的xml

<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.

我看不到 getX() getY()函数有任何问题,因为它们实际上告诉我图像从哪里开始.

I don't see any problem with the getX() and getY() functions, because they actually show me where does the image begin.

文档说明, getWidth() getHeight 方法返回视图的宽度和高度,但是监视窗口告诉我 getWidth() getHeight 是300,我真的不明白,因为在我的XML中,我分别将它们设置为100dp,因此函数以不同的度量将它们返回给我,并且如何将其转换为dp.

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.

最后,它告诉我 getTop() getLeft 分别是700和300,正如文档所说,它们是图像相对于图像的位置父母但是我父母不是线性布局,那么这个数字在屏幕定位方面意味着什么?

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?

推荐答案

所有这些测量方法均以像素( px )为单位返回尺寸,而不是以密度像素( dp )为单位返回尺寸.如果要进行转换,可以通过调用以下命令获取密度:

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

这篇关于Android视图的getTop(),getLeft(),getX(),getY(),getWidth(),getHeight()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆