无论高度如何,我都想让 imageView 成为一个完美的正方形
I want to have an imageView be a perfect square regardless of the height
我在行布局中有一个 imageView
,用于 recyclerView
。这个 imageView
的 height 设置为 match_parent
,但为了让它成为一个完美的正方形,我不能硬编码 width 的 imageView
。
我需要的是一种将 imageView
的 width 设置为与 height 完全相同的方法imageView
显示时的宽度,因为将宽度设置为 wrap_content
或 match_parent
会抛出布局的其余部分,因为它是矩形的、相当大的图像。
如有任何帮助,我们将不胜感激。
你可以这样做:
public class FixedAspectRatioFrameLayout extends FrameLayout {
private float ratio;
public FixedAspectRatioFrameLayout(@NonNull Context context) {
super(context);
}
public FixedAspectRatioFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attributeSet) {
fillFromAttrs(context, attributeSet);
}
private void fillFromAttrs(Context context, AttributeSet attributeSet) {
TypedArray array = context.obtainStyledAttributes(attributeSet, R.styleable.FixedAspectRatioFrameLayout);
ratio = array.getFloat(R.styleable.FixedAspectRatioFrameLayout_ratio, 0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
int finalWidth = originalWidth;
int finalHeight = originalHeight;
if (ratio != 0) {
if (originalHeight == 0) {
finalHeight = (int) (originalWidth / ratio);
} else if (originalWidth == 0) {
finalWidth = (int) (originalHeight * ratio);
}
}
super.onMeasure(
MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY)
);
}
}
您还需要在 res/values/attrs 中指定属性 "ratio"。xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FixedAspectRatioFrameLayout">
<attr name="ratio" format="float"/>
</declare-styleable>
</resources>
现在您可以根据需要指定此 FrameLayout 的高度,将宽度设置为 0dp,将比率设置为 1,然后将您的 ImageView 放入此 FrameLayout
此外,如果您在 ConstraintLayout 中设置高度以匹配约束,则可以为此视图指定比例:
我在行布局中有一个 imageView
,用于 recyclerView
。这个 imageView
的 height 设置为 match_parent
,但为了让它成为一个完美的正方形,我不能硬编码 width 的 imageView
。
我需要的是一种将 imageView
的 width 设置为与 height 完全相同的方法imageView
显示时的宽度,因为将宽度设置为 wrap_content
或 match_parent
会抛出布局的其余部分,因为它是矩形的、相当大的图像。
如有任何帮助,我们将不胜感激。
你可以这样做:
public class FixedAspectRatioFrameLayout extends FrameLayout {
private float ratio;
public FixedAspectRatioFrameLayout(@NonNull Context context) {
super(context);
}
public FixedAspectRatioFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attributeSet) {
fillFromAttrs(context, attributeSet);
}
private void fillFromAttrs(Context context, AttributeSet attributeSet) {
TypedArray array = context.obtainStyledAttributes(attributeSet, R.styleable.FixedAspectRatioFrameLayout);
ratio = array.getFloat(R.styleable.FixedAspectRatioFrameLayout_ratio, 0);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
int finalWidth = originalWidth;
int finalHeight = originalHeight;
if (ratio != 0) {
if (originalHeight == 0) {
finalHeight = (int) (originalWidth / ratio);
} else if (originalWidth == 0) {
finalWidth = (int) (originalHeight * ratio);
}
}
super.onMeasure(
MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY)
);
}
}
您还需要在 res/values/attrs 中指定属性 "ratio"。xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FixedAspectRatioFrameLayout">
<attr name="ratio" format="float"/>
</declare-styleable>
</resources>
现在您可以根据需要指定此 FrameLayout 的高度,将宽度设置为 0dp,将比率设置为 1,然后将您的 ImageView 放入此 FrameLayout
此外,如果您在 ConstraintLayout 中设置高度以匹配约束,则可以为此视图指定比例: