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

问题描述:

我有一个GridView与ImageViews里面。我每行有3个。我可以使用WRAP_CONTENT和scaleType = CENTER_CROP正确设置宽度,但我不知道如何将ImageView的大小设置为正方形。这里就是我所做的到现在为止,这似乎是确定的,除了高度,那就是“静”:ImageView是一个具有动态宽度的正方形吗?

imageView = new ImageView(context);  
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300)); 

我正在做一个转接器内。

最好的办法是自己继承ImageView,覆盖措施通:

public class SquareImageView extends ImageView { 

    ... 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int width = getMeasuredWidth(); 
    setMeasuredDimension(width, width); 
    } 

    ... 

} 
+0

太棒了!非常感谢你,我没有想到这种可能性 – 2013-05-12 13:38:40

+0

如果我设置了填充//边距,我得到一个白色的边框。任何想法如何解决这个问题? – 2013-08-04 13:25:01

+2

android:adjustViewBounds =“true” android:scaleType =“centerCrop”@Waza_Be – 2014-03-05 04:55:58

对方回答工作正常。这只是bertucci解决方案的一个扩展,使得ImageView的方形宽度和高度与xml充气布局相关。

创建一个类,说SquareImageView延伸的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, heightMeasureSpec); 

     int width = getMeasuredWidth(); 
     setMeasuredDimension(width, width); 
    } 

} 

现在,在你的XML做到这一点,

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

如果您不需要在程序中动态创建的ImageView ,而是要在xml中修复,那么这个实现将会很有帮助。

+1

太棒了,谢谢Andro。这正是我所期待的。再次感谢您花时间提供完整的答案。 – Taliadon 2013-10-24 17:16:42

+1

这应该被标记为正确的完整解决方案。 – JRod 2014-07-30 00:41:21

+0

,对我 – 2016-06-21 17:26:07

更简单:

public class SquareImageView extends ImageView { 
    ... 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    }  
} 
+0

没有为SDK> = 21 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 公共SquareImageView(上下文的背景下,ATTRS的AttributeSet,诠释defStyleAttr,诠释defStyleRes){ 超(背景下,ATTRS忘记工作, defStyleAttr,defStyleRes); } – 2016-03-14 12:35:21

+1

@JanRabe Thx,已更新。 – mixel 2016-03-14 13:24:37

一些以前的答案是完全足够了。我只是在@Andro Selva和@ a.bertucci的解决方案中添加一个小的优化:

这是一个很小的优化,但检查宽度和高度是否不同可能会阻止另一次测量。

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(); 
     int height = getMeasuredHeight(); 

     // Optimization so we don't measure twice unless we need to 
     if (width != height) { 
      setMeasuredDimension(width, width); 
     } 
    } 

} 

这里所有都是不必要的,调用它的超类onMeasure。这里是我的实施

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 
     int size = Math.min(width, height); 
     setMeasuredDimension(size, size); 
    }