正方形的ImageView

10

我想在对话框中展示一个总是呈正方形的 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图像大小的矩形。我该怎么做才能达到相同的效果?

编辑:为了让你了解我尝试实现的效果,就像在通讯录应用程序中选择联系人并单击个人资料图片时显示的内容。 它会缩小并保持为顶部的正方形。

4个回答

13

在运行时设置图像视图的高度,但首先使用此代码获取显示宽度

Display  display = getWindowManager().getDefaultDisplay();
int swidth = display.getWidth();

现在请按照以下方式设置图像视图的高度

LayoutParams params = eventImage.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.height = swidth ;
eventImage.setLayoutParams(params);

3

您可以使用:

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


3
创建一个单独的类来动态定义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" />

0

创建自己的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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接