简体   繁体   English

为什么 view.height 比实际高度大 3 倍?

[英]Why view.height is 3 times bigger than actual height?

I have an ImageView with height set to 120dp.我有一个高度设置为 120dp 的 ImageView。 When I get the view.height it equalls to 360, why won't it be equal to 120?当我得到 view.height 时,它等于 360,为什么它不等于 120?

I'm not sure of the code won't be of help but:我不确定代码不会有帮助,但是:

    <ImageView
        android:id="@+id/profilePic1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"
        app:srcCompat="@drawable/profile_pic1"/>

Activity:活动:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val layoutInflater: LayoutInflater = LayoutInflater.from(this)
    val view: View = layoutInflater.inflate(R.layout.activity_edit_profile, null)

    view.viewTreeObserver.addOnGlobalLayoutListener(object: ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            // remove listener
            view.viewTreeObserver.removeOnGlobalLayoutListener(this)

            // insert coordinates (coor) of each image to imagePosition variables
            Log.d("TAG", profilePic1.height.toString()) // RETURNS 360

        }
    })

Because in xml file you have given size in DP and In java that parameter returns values in Pixel , Please check this for more understanding about DP and PIxel因为在 xml 文件中,您在 DP 中给出了大小,而在 java 中,该参数以 Pixel 返回值,请检查此以了解有关 DP 和 PIxel 的更多信息

https://developer.android.com/training/multiscreen/screendensities https://developer.android.com/training/multiscreen/screendensities

36x36 (0.75x) for low-density (ldpi)
48x48 (1.0x baseline) for medium-density (mdpi)
72x72 (1.5x) for high-density (hdpi)
96x96 (2.0x) for extra-high-density (xhdpi)
144x144 (3.0x) for extra-extra-high-density (xxhdpi)
192x192 (4.0x) for extra-extra-extra-high-density (xxxhdpi)

This is just because you are using XXHDPI device, where 120dp is being converted to 360PX.这只是因为您使用的是XXHDPI设备,其中 120dp 正在转换为 360PX。

Must read What is the difference between "px", "dip", "dp" and "sp"?必读“px”、“dip”、“dp”和“sp”有什么区别?


You should read Image resolution for mdpi, hdpi, xhdpi and xxhdpi to understand how DP and pixels work together.您应该阅读mdpi、hdpi、xhdpi 和 xxhdpi 的图像分辨率以了解 DP 和像素如何协同工作。 And official documentation can be found here官方文档可以在这里找到


If I explain it in short, in MDPI 1dp is almost equals to 1PX.如果我简单地解释一下,在MDPI 1dp 几乎等于 1PX。 So if you check your code on devices of MDPI resolution you will get 120PX output.因此,如果您在MDPI分辨率的设备上检查代码,您将获得120PX输出。 But as I added a URL above for other resolutions it follows a rule of conversion from dp to PX.但是当我在上面为其他分辨率添加了一个 URL 时,它遵循从 dp 到 PX 的转换规则。 It would give you 180px in HDPI, 240px in XHDPI, 360px in XXHDPI, 480px in XXXHDPI devices as output.它会给你180px in HDPI, 240px in XHDPI, 360px in XXHDPI, 480px in XXXHDPI设备作为输出。

In java you get the height in px instead of dp .在 java 中,您以px而不是dp获得高度。 You have to convert it to dp to get correct data:您必须将其转换为dp才能获得正确的数据:

float px = profilePic1.height;
float dp = px / context.getResources().getDisplayMetrics().density;

// here dp should be 120

有一个名为sdpssp的库,用于使维度具有响应性。

Because you have given height in dp(density pixel) while in the code when you retrieve it , it is retrieved in pixels only.因为您在检索它时在代码中以 dp(密度像素)为单位给出了高度,所以它仅以像素为单位进行检索。 You have to convert it to dp.您必须将其转换为 dp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么滚动视图的高度比实际内容长 - Why the height of the scroll view is longer than the actual content 为什么布局高度大于预期 - Why Layout height is bigger than expected android-加载的图像的高度大于实际图像 - android - loaded image's height it bigger than actual image 如何判断ListView项目的高度是否大于实际ListView的高度(即列表视图是否可以滚动)? - How to tell if height of ListView items is bigger than heigh of actual ListView (i.e. can the list view be scrolled)? 获取实际的视图高度 - Get Actual Height of View SmoothAppBarLayout折叠的工具栏高度大于预期的高度 - SmoothAppBarLayout collapsed toolbar height is bigger than intended Android:ConstraintLayout 高度大于模拟器屏幕高度 - Android: ConstraintLayout height is bigger than emulator screen's height 为什么camera2支持的预览尺寸宽度总是大于高度? - Why camera2 supported preview size width always bigger than height? 为什么我不能将布局的高度扩展到超过“ MATCH_PARENT” - Why I can't expand my layout's height bigger than 'MATCH_PARENT' 适用于高度大于宽度的屏幕的 Android Studio 布局资源 - Android studio layout resource for screens with height bigger than width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM