2

I have spent a very long time researching this issue to no avail. I am attempting to automatically resize an ImageView to match the parent views width (maintaining aspect ratio), while at the same time only allowing the top of the image to be cropped. The left, right, and bottom portions of the image cannot be cut off, so centerCrop does not accomplish what I need.

The top of the image fades into the background color of the app, which is the only part of the image that can be cropped to achieve a proper aspect ratio.

I have also tried using fitXY, however, the aspect ratio is not kept and it looks very bad on some devices.

Arrexel
  • 55
  • 6

2 Answers2

0

I have solved this issue. Using the custom ImageView class posted by Mark Martinsson (answer here https://stackoverflow.com/a/17424313/2040690), and setting the ImageView to alignParentBottom, the image will fit perfectly inside the view while maintaining aspect ratio, and the excess portion of the image on the top will simply extend out of the screen boundary, essentially "cropping" it.

Community
  • 1
  • 1
Arrexel
  • 55
  • 6
0

Using Glide, I created a transformation to simply make the bitmap square

Glide.with(view.getContext()).load(someUrl))
    .transform(new BitmapTransformation(view.getContext()) {
        @Override
        protected Bitmap transform(BitmapPool pool, 
                          Bitmap toTransform, int outWidth, 
                          int outHeight) {
            return Bitmap.createBitmap(toTransform, 0, 0, 
                       toTransform.getWidth(), toTransform.getWidth());
        }

    @Override
    public String getId() {
        return "some_unique_id";
    }
}).into(imageView);
Boy
  • 7,010
  • 4
  • 54
  • 68