Android UI设计自动重新调整ImageView大小



ImageView获取一个图像并包装内容,因此如果图像的尺寸为100x100,则ImageView有相同的尺寸。我只想指定此ImageViewlayout_height,并且当它匹配图像以正确地重新调整大小时,ImageView的layout_width

这里有一个例子:

图像大小=100x100;

ImageViewlayout_height=200;

ImageViewlayout_width应变为200而不是100。

这可能吗?

您需要创建一个自定义ImageView并覆盖onMeasure以使视图始终为方形。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    SQUARE_DIMENSION  = this.getMeasuredHeight(); // whatever height you want here
    this.setMeasuredDimension(SQUARE_DIMENSION, SQUARE_DIMENSION);
}

最新更新