我试图在对话框中显示一个始终呈正方形的ImageView
。尺寸可能因显示器的分辨率而异(准确地说是纵向宽度),但需要 ImageView 形成尽可能大的正方形以容纳其中的正方形图像。这是我的XML代码。
<ImageView
android:id="@+id/dialog_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true"
/>
但问题是Square
图像是在运行时动态设置的,ImageView 最终是一个矩形,其宽度与方形图像大小相同。我能做些什么来实现同样的目标?
编辑:要了解我想要实现的目标,就像在联系人应用程序中选择联系人并单击个人资料图片时显示的内容一样。它会缩小并保留为活动顶部的正方形。
在运行时设置图像视图高度,但首先使用此代码获取显示宽度
Display display = getWindowManager().getDefaultDisplay();
int swidth = display.getWidth();
现在像这样设置图像视图的高度
LayoutParams params = eventImage.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.height = swidth ;
eventImage.setLayoutParams(params);
创建一个单独的类,用于动态定义应继承 ImageView 类的 ImageView 的大小,如下所示:
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = getMeasuredWidth();
setMeasuredDimension(width, width);
}
}
setMeasuredDimension(width, width);会自动将高度设置为宽度。
在 xml 文件中,使用此类作为视图代替 ImageView,如下所示:
<com.packagepath.SquareImageView
android:id="@+id/Imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
:
app:layout_constraintDimensionRatio="1:1"
这样:
<android.support.constraint.ConstraintLayout
android:id="@+id/lyt_img_cover"
android:layout_width="@dimen/image_top_episode"
android:layout_height="match_parent"
android:layout_centerInParent="false"
android:elevation="0dp"
android:foregroundGravity="center">
<ImageView
android:id="@+id/img_episode"
android:layout_width="0dp"
android:layout_height="0dp"
android:elevation="7dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</ImageView>
其布局应为约束布局
创建自己的ImageView
,该应继承 ImageView 类,如下所示:
public class SquareImageView extends AppCompatImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size;
//If the layout_width or layout_width of this view is set as match_parent or any exact dimension, then this view will use that dimension
if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY ^ MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY)
size = width;
else
size = height;
}
//If the layout_width and layout_width of this view are not set or both set as match_parent or any exact dimension,
// then this view will use the minimum dimension
else
size = Math.min(width, height);
setMeasuredDimension(size, size);
}
}