0

An ImageView gets an image and wraps the content so if the image has dimensions like 100x100, the ImageView has the same dimensions. I want to specify only the layout_height of this ImageView and when it matches the image to re-size properly the layout_width of the ImageView.

Here is an example:

image size = 100x100;

ImageView's layout_height = 200;

ImageView's layout_width should become 200 not 100.

Is this possible?

nenito
  • 1,214
  • 6
  • 19
  • 33

1 Answers1

1

You'd need to create a custom ImageView and override onMeasure to make the view always square.

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

    SQUARE_DIMENSION  = this.getMeasuredHeight(); // whatever height you want here

    this.setMeasuredDimension(SQUARE_DIMENSION, SQUARE_DIMENSION);
}
  • You are taking my example as a private case. You can have also image with size 100x300 then if layout_width = 200, layout_height should be 600 --> ImageView 200x600. I'm giving example with easy to calculate integers but it could be more complex. I hope you are getting my point now. – nenito Jul 18 '13 at 22:33
  • 1
    Well my example still stands in that if you want to control exactly how you size a view you need to override onMeasure. What you use to determine the size is up to you. In your case it sounds like you are trying to keep the aspect ratio of the image when scaling the image view up or down. If that is the case then you need to grab the image size and adjust the code accordingly. – metalmonkeysoftware Jul 19 '13 at 12:09