code: private RectF generateRandomRect(RectF drawableBounds, RectF viewportRect) {
          float drawableRatio = MathUtils.getRectRatio(drawableBounds);
          float viewportRectRatio = MathUtils.getRectRatio(viewportRect);
          RectF maxCrop;
  
          if (drawableRatio > viewportRectRatio) {
              float r = (drawableBounds.height() / viewportRect.height()) * viewportRect.width();
              float b = drawableBounds.height();
              maxCrop = new RectF(0, 0, r, b);
          } else {
              float r = drawableBounds.width();
              float b = (drawableBounds.width() / viewportRect.width()) * viewportRect.height();
              maxCrop = new RectF(0, 0, r, b);
          }
  
          float randomFloat = MathUtils.truncate(mRandom.nextFloat(), 2);
          float factor = MIN_RECT_FACTOR + ((1 - MIN_RECT_FACTOR) * randomFloat);
  
          float width = factor * maxCrop.width();
          float height = factor * maxCrop.height();
          int widthDiff = (int) (drawableBounds.width() - width);
          int heightDiff = (int) (drawableBounds.height() - height);
          int left = widthDiff > 0 ? mRandom.nextInt(widthDiff) : 0;
          int top = heightDiff > 0 ? mRandom.nextInt(heightDiff) : 0;
          return new RectF(left, top, left + width, top + height);
      }
docs: Generates a random rect that can be fully contained within {@code drawableBounds} and
  has the same aspect ratio of {@code viewportRect}. The dimensions of this random rect
  won't be higher than the largest rect with the same aspect ratio of {@code viewportRect}
  that {@code drawableBounds} can contain. They also won't be lower than the dimensions
  of this upper rect limit weighted by {@code MIN_RECT_FACTOR}.
  @param drawableBounds the bounds of the drawable that will be zoomed and panned.
  @param viewportRect the bounds of the view that the drawable will be shown.
  @return an arbitrary generated rect with the same aspect ratio of {@code viewportRect}
  that will be contained within {@code drawableBounds}.