如何获得在android中的imageView中使用的任何图像的高度和宽度

时间:2017-01-23 09:48:57

标签: android android-layout android-cardview recyclerview-layout

我需要一个解决方案,如何找到任何可以完全适合ImageView高度和宽度的图像高度和宽度?

2 个答案:

答案 0 :(得分:1)

您可以通过其drawable获取图像宽度和高度;

int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();

如果您希望图像适合ImageView,则可以使用

<ImageView
    android:id="@id/img"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY" />

但是你有一张低分辨率的图像然后会扭曲。

答案 1 :(得分:0)

如果您希望图像适合图像视图而不会出现图像失真

<ImageView
    android:id="@id/img"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter" />