Android 方形图像视图
我试图在对话框中显示一个始终为方形的Android 方形图像视图,android,android-layout,android-imageview,Android,Android Layout,Android Imageview,我试图在对话框中显示一个始终为方形的ImageView。尺寸可能因显示器的分辨率(精确地说是纵向宽度)而有所不同,但ImageView需要形成最大的正方形,以容纳其中的正方形图像。这是我的XML代码 <ImageView android:id="@+id/dialog_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleTyp
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最终是一个宽度与Square图像大小相同的矩形。我能做些什么来实现同样的目标
编辑:要了解我想要实现的目标,就像在“联系人”应用程序中选择联系人并单击个人资料图片时所显示的一样。它将缩小并保持为活动顶部的正方形。在运行时设置图像视图高度,但首先使用此代码获取显示宽度
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);
}
<com.packagepath.SquareImageView
android:id="@+id/Imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
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);
}
}
}
设置的测量尺寸(宽度、宽度);将自动将您的高度设置为宽度
在xml文件中,将此类用作视图,以代替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);
}
<com.packagepath.SquareImageView
android:id="@+id/Imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
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);
}
}
创建您自己的应继承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);
}
<com.packagepath.SquareImageView
android:id="@+id/Imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
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);
}
}
您可以使用:
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>
它的布局应该是ConstraintLayout非常感谢您的回复。工作起来很有魅力。