简体   繁体   中英

Why view.height is 3 times bigger than actual height?

I have an ImageView with height set to 120dp. When I get the view.height it equalls to 360, why won't it be equal to 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

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.

Must read What is the difference between "px", "dip", "dp" and "sp"?


You should read Image resolution for mdpi, hdpi, xhdpi and xxhdpi to understand how DP and pixels work together. And official documentation can be found here


If I explain it in short, in MDPI 1dp is almost equals to 1PX. So if you check your code on devices of MDPI resolution you will get 120PX output. But as I added a URL above for other resolutions it follows a rule of conversion from dp to PX. It would give you 180px in HDPI, 240px in XHDPI, 360px in XXHDPI, 480px in XXXHDPI devices as output.

In java you get the height in px instead of dp . You have to convert it to dp to get correct data:

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. You have to convert it to dp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM