Square ImageView

时间:2012-11-29 10:13:13

标签: 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:scaleType="centerInside"
    android:adjustViewBounds="true"
    />

但问题是Square图像是在运行时动态设置的,而ImageView最终是一个宽度为Square Image size的矩形。我能做些什么来实现同样的目标?

编辑:要了解我要实现的目标,就像在联系人应用程序中选择联系人并单击个人资料图片时所显示的内容一样。它缩小并保持在活动顶部的正方形。

4 个答案:

答案 0 :(得分:13)

在运行时设置图像视图高度,但首先使用此代码

获取显示宽度
Display  display = getWindowManager().getDefaultDisplay();
int swidth = display.getWidth();

现在设置像这样的图像视图的高度

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

答案 1 :(得分: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(宽度,宽度);将自动将您的身高设置为宽度。

在xml文件中,使用此类作为视图来代替ImageView,如下所示:

  <com.packagepath.SquareImageView
        android:id="@+id/Imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

答案 2 :(得分:1)

您可以使用:

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 :(得分: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);
    }
}