gpt4 book ai didi

android - 如何在 Android 中为 ImageView 设置最小和最大尺寸

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:42:05 25 4
gpt4 key购买 nike

我想指定两个最小值。宽度/高度和最大。 ImageView 的宽度/高度。图像应缩放以适合。

这样做:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:maxWidth="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:src="@drawable/ic_launcher" />

最小值。 width 和 height 表现正常,但最大。忽略宽度和高度。

如果我设置 android:adjustViewBounds="true":

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:minWidth="100dp"
android:maxWidth="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:src="@drawable/ic_launcher" />

比,最大。 width 和 height 表现正常,但最小值。忽略宽度和高度。

有没有办法兼得?

最佳答案

最后,我创建了自己的 View ,它扩展了 ImageView 并覆盖了 onMeasure():

public class ResizingImageView extends ImageView {

private int mMaxWidth;
private int mMaxHeight;

public ResizingImageView(Context context) {
super(context);
}

public ResizingImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ResizingImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void setMaxWidth(int maxWidth) {
super.setMaxWidth(maxWidth);
mMaxWidth = maxWidth;
}

@Override
public void setMaxHeight(int maxHeight) {
super.setMaxHeight(maxHeight);
mMaxHeight = maxHeight;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

Drawable drawable = getDrawable();
if (drawable != null) {

int wMode = MeasureSpec.getMode(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
if (wMode == MeasureSpec.EXACTLY || hMode == MeasureSpec.EXACTLY) {
return;
}

// Calculate the most appropriate size for the view. Take into
// account minWidth, minHeight, maxWith, maxHeigh and allowed size
// for the view.

int maxWidth = wMode == MeasureSpec.AT_MOST
? Math.min(MeasureSpec.getSize(widthMeasureSpec), mMaxWidth)
: mMaxWidth;
int maxHeight = hMode == MeasureSpec.AT_MOST
? Math.min(MeasureSpec.getSize(heightMeasureSpec), mMaxHeight)
: mMaxHeight;

int dWidth = Helpers.dipsToPixels(drawable.getIntrinsicWidth());
int dHeight = Helpers.dipsToPixels(drawable.getIntrinsicHeight());
float ratio = ((float) dWidth) / dHeight;

int width = Math.min(Math.max(dWidth, getSuggestedMinimumWidth()), maxWidth);
int height = (int) (width / ratio);

height = Math.min(Math.max(height, getSuggestedMinimumHeight()), maxHeight);
width = (int) (height * ratio);

if (width > maxWidth) {
width = maxWidth;
height = (int) (width / ratio);
}

setMeasuredDimension(width, height);
}
}
}

这个 View 现在可以像这样使用:

<my.package.ResizingImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:maxWidth="200dp"
android:minHeight="100dp"
android:maxHeight="200dp"
android:src="@drawable/ic_launcher" />

这如我所愿,但难道不应该有更简单的解决方案吗?

关于android - 如何在 Android 中为 ImageView 设置最小和最大尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9914810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com