Glide는 좋은 라이브러리이지만 이미지 뷰에서 이미지를 뽑는 것이 쉽지 않습니다. 이미지 뷰가 DrawableBitmap을 가지지 않고 다양한 Drawable을 가지기 때문입니다.

이미지가 캐쉬에 있는 경우에는 GlideBitmapDrawable을 가질 수 있습니다. 그렇지 않은 경우에는 TransitionDrawable을 가질 수도 있는데 이 경우에는 레이어에 GlideBitmapDrawable을 직접 가지는 경우와 SquaringDrawable을 가지는 경우를 보았습니다.

  1. GlideBitmapDrawable는 실제 비트맵을 가지고 있는 Drawable입니다.
  2. TransitionDrawable는 아직 로드가 되지 않은 비트맵에 대해 에니메이션을 제공하기 위한 Drawable입니다.
  3. SquaringDrawable은 래핑 객체로 정사각형을 위한 객체로 보입니다. (확인하지 않음.)

이 경우들을 상정하고 작성한 코드는 아래와 같습니다. instanceOf가 도배되어 좀 더러운 코드입니다.

Bitmap bitmap = null;
Drawable drawable = imageCard.getImageView().getDrawable();
if (drawable instanceof GlideBitmapDrawable) {
    bitmap = ((GlideBitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof TransitionDrawable) {
    TransitionDrawable transitionDrawable = (TransitionDrawable) drawable;
    int length = transitionDrawable.getNumberOfLayers();
    for (int i = 0; i < length; ++i) {
        Drawable child = transitionDrawable.getDrawable(i);
        if (child instanceof GlideBitmapDrawable) {
            bitmap = ((GlideBitmapDrawable) child).getBitmap();
            break;
        } else if (child instanceof SquaringDrawable
                && child.getCurrent() instanceof GlideBitmapDrawable) {
            bitmap = ((GlideBitmapDrawable) child.getCurrent()).getBitmap();
            break;
        }
    }
} else if (drawable instanceof SquaringDrawable) {
    bitmap = ((GlideBitmapDrawable) drawable.getCurrent()).getBitmap();
}

Glide가 생성하는 모든 Drawable을 고려한 것이 아니기 때문에 빠져있는 케이스가 존재할지도 모르겠는데 제가 쓰는 케이스에서는 이 정도면 대부분은 커버하는 것 같습니다.

이왕 커스텀 Drawable을 쓴다면 Bitmap을 좀 더 손쉽게 추출할 수 있는 방법을 제공해줬으면 더 좋았을 것 같은데 조금 아쉽네요.