0

So to make an image square the following code works:

public class SquareImageView  extends ImageView {

  ...

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

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

  ...

}

So my question is:
If I understand correctly we have to call super.onMeasure so that the object can figure out its width and height. But when we pass in the setMeasuredDimension the width both as height and width to make the image square does the Android scale the image?
Any kind of such changes are they scaled properly? Or the best approach is to already have the resource square e.g. from some photo editing tool

Jim
  • 18,826
  • 34
  • 135
  • 254
  • `does the Android scale the image?` No, `setMeasuredDimension` is non-wiser about the `ImageView`, it just specifies the boundaries of the `View`. – azizbekian Oct 10 '17 at 10:41
  • @azizbekian:So how come the image shows up perfect even though I forced it to become square with that code? I don't understand this – Jim Oct 10 '17 at 13:47
  • 1
    `ImageView` has a default `scaleType` center, thus it will center your image into the view and crop all excessive parts that do not fit in the boundaries. You can experiment with different scale types and see the output. – azizbekian Oct 10 '17 at 14:03
  • @azizbekian:Ah so the image is already pre-processed/scaled and this just crops out based on width/height? – Jim Oct 11 '17 at 08:11
  • I would say the other way around: you dictate the width/height of the view, then inside the `onDraw()` based on the `scaleType` of the `ImageView` it would be fitted in the boundaries of the `View`. – azizbekian Oct 11 '17 at 13:25

0 Answers0