我有一个图像视图,如下所示:
<ImageView
android:id="@+id/add_image_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/other_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
app:srcCompat="@drawable/add_button" />
我正在尝试使用布局参数以编程方式(基于设备大小)进行缩放。像这样的东西很好用:
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
val addImage = findViewById<ImageView>(R.id.add_image_signup)
val layoutParams = addImage.getLayoutParams()
layoutParams.width = width * 0.3
layoutParams.height = height * 0.1
addImage.setLayoutParams(layoutParams)
并缩放图像。但是,我只想缩放宽度,并相应地缩放高度,以使图像保持其纵横比。
我尝试只缩放宽度,如下所示:
val addImage = findViewById<ImageView>(R.id.add_image_signup)
val layoutParams = addImage.getLayoutParams()
layoutParams.width = width * 0.3
addImage.setLayoutParams(layoutParams)
但这似乎没有效果。
有没有办法只指定宽度值,并相应地调整高度以使图像保持其纵横比?
谢谢