ImageView是具有动态宽度的正方形吗?

我有一个带有ImageViews的GridView。我每行有3个。我可以使用WRAP_CONTENT和scaleType = CENTER_CROP正确设置宽度,但是我不知道如何将ImageView的大小设置为正方形。这是我到目前为止所做的,除了高度,它是“静态的”,似乎还可以:


imageView = new ImageView(context);     

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300));

我正在适配器内做。


慕妹3146593
浏览 768回答 3
3回答

摇曳的蔷薇

最好的选择是继承ImageView自己的子类,以覆盖度量传递:public class SquareImageView  extends ImageView {  ...  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    int width = getMeasuredWidth();    setMeasuredDimension(width, width);  }  ...}

九州编程

另一个答案很好。这只是bertucci解决方案的扩展,以使ImageView相对于xml膨胀版式具有正方形的宽度和高度。创建一个类,说一个SquareImageView这样扩展ImageView,public class SquareImageView extends ImageView {&nbsp; &nbsp; public SquareImageView(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public SquareImageView(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public SquareImageView(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {&nbsp; &nbsp; &nbsp; &nbsp; super.onMeasure(widthMeasureSpec, heightMeasureSpec);&nbsp; &nbsp; &nbsp; &nbsp; int width = getMeasuredWidth();&nbsp; &nbsp; &nbsp; &nbsp; setMeasuredDimension(width, width);&nbsp; &nbsp; }}现在,在您的xml中执行此操作,&nbsp; &nbsp; &nbsp; &nbsp; <com.packagepath.tothis.SquareImageView&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/Imageview"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="fill_parent" />如果您需要不在程序中动态创建ImageView而不是在xml中进行固定,那么此实现将非常有用。

莫回无

前面的几个答案就足够了。我只是在这里为@Andro Selva和@ a.bertucci的解决方案添加一个小的优化:这是一个很小的优化,但是检查宽度和高度是否不同可以防止再次进行测量。public class SquareImageView extends ImageView {&nbsp; &nbsp; public SquareImageView(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public SquareImageView(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; }&nbsp; &nbsp; public SquareImageView(Context context, AttributeSet attrs, int defStyle) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {&nbsp; &nbsp; &nbsp; &nbsp; super.onMeasure(widthMeasureSpec, widthMeasureSpec);&nbsp; &nbsp; &nbsp; &nbsp; int width = getMeasuredWidth();&nbsp; &nbsp; &nbsp; &nbsp; int height = getMeasuredHeight();&nbsp; &nbsp; &nbsp; &nbsp; // Optimization so we don't measure twice unless we need to&nbsp; &nbsp; &nbsp; &nbsp; if (width != height) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setMeasuredDimension(width, width);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android