[英]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
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.