2012-11-29 41 views
10

我试图展示一个ImageView,它在对话框中始终是正方形的。尺寸可能会根据显示器的分辨率(精确的纵向宽度)而有所不同,但ImageView需要形成最大的Square,以容纳其中的方形图像。这是我的XML代码。Square ImageView

<ImageView 
    android:id="@+id/dialog_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="centerInside" 
    android:adjustViewBounds="true" 
    /> 

但问题是,Square图像是在运行时动态地设置,并且ImageView的结束是用广度为正方形图像大小的矩形。我能做些什么来达到同样的效果?

编辑:为了给什么,我想实现像当联系人应用程序,并单击该资料图片被选中的联系人显示什么的想法。它缩小并保持为活动顶部的一个正方形。

回答

13

设置图像视图高度在运行时间,但第一与此代码

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

得到显示宽度和现在设置图像视图的高度这样

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

非常感谢你很多回应。奇迹般有效。 – Shakti

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

非常感谢,这工作很不错 –

+0

乌尔欢迎.. :) – HAXM

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); 
    } 
}