1

I have a fixed size ImageView. I want to achieve this :

  1. Scale the Bitmap always to the width, if the Bitmap is wider or smaller than Imageview width.
  2. Crop the height if taller than ImageView height else scale it to the height.

I want something like this answer, but the other way around (FitXCropY). I have tried changing this answer with no success.

Thank you.

Community
  • 1
  • 1
quad
  • 872
  • 3
  • 17
  • 37

2 Answers2

1

Based on this answer ImageView to scale to fixed height, but crop excess width

public class FitXCropYImageView extends ImageView {
boolean done = false;

@SuppressWarnings("UnusedDeclaration")
public FitXCropYImageView(Context context) {
    super(context);
    setScaleType(ScaleType.MATRIX);
}

@SuppressWarnings("UnusedDeclaration")
public FitYCropXImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setScaleType(ScaleType.MATRIX);
}

@SuppressWarnings("UnusedDeclaration")
public FitXCropYImageView(Context context, AttributeSet attrs, int defStyle)      {
    super(context, attrs, defStyle);
    setScaleType(ScaleType.MATRIX);
}

private final RectF drawableRect = new RectF(0, 0, 0,0);
private final RectF viewRect = new RectF(0, 0, 0,0);
private final Matrix m = new Matrix();
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (done) {
        return;//Already fixed drawable scale
    }
    final Drawable d = getDrawable();
    if (d == null) {
        return;//No drawable to correct for
    }
    int viewHeight = getMeasuredHeight();
    int viewWidth = getMeasuredWidth();
    int drawableWidth = d.getIntrinsicWidth();
    int drawableHeight = d.getIntrinsicHeight();
    drawableRect.set(0, 0, drawableWidth, drawableHeight);//Represents the original image
    //Compute the left and right bounds for the scaled image
    float viewHalfHeight = viewHeight / 2;
    float scale = (float) viewWidth / (float) drawableWidth;
    float scaledHeight = drawableHeight * scale;
    float scaledHalfHeight = scaledHeight / 2;
    viewRect.set(0, viewHalfHeight-scaledHalfHeight,viewWidth, viewHalfHeight+scaledHalfHeight);

    m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER /* This constant doesn't matter? */);
    setImageMatrix(m);

    done = true;

    requestLayout();
}
}
Community
  • 1
  • 1
Nishant
  • 575
  • 4
  • 15
  • 1
    Does not work. Smaller images are cropped and scaled to fit the screen. I want the image to be cropped only if the height is larger than imageview height. All other cases the image should be scaled up or down. – quad Jun 26 '15 at 10:20
  • You can use Matrix.ScaleToFit.FILL if the image height is less than your imageview's height and use Matrix.ScaleToFit.CENTER if is it's bigger than imageview's height. – Nishant Jun 26 '15 at 11:46
  • Is working for my use case of cropping larger images – Jonathan Dunn Jul 05 '18 at 12:40
-1

Try like this,

Add inside Oncreate

viewImage = (ImageView) findViewById(R.id.pictureImageview123);
viewImage.setScaleType(ScaleType.FIT_XY);
Android Dev
  • 421
  • 8
  • 26
  • Thanks , but I think FIT_XY will scale down the Bitmap height if it is taller than ImageView height, but I want it to be cropped. – quad Jun 26 '15 at 07:22