Skip to content

Instantly share code, notes, and snippets.

@alexzaitsev
Created September 13, 2017 07:33
Show Gist options
  • Save alexzaitsev/0c16fdffa7c966aa822e41955eee5a0b to your computer and use it in GitHub Desktop.
Save alexzaitsev/0c16fdffa7c966aa822e41955eee5a0b to your computer and use it in GitHub Desktop.
public static void loadAndCropImage(Context context, @DrawableRes int resourceId, final int smallerSize, ImageView imageView) {
RequestOptions transformation = RequestOptions.bitmapTransform(new Transformation<Bitmap>() {
@Override
public Resource<Bitmap> transform(Context context, Resource<Bitmap> resource, int outWidth, int outHeight) {
BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
Bitmap toTransform = resource.get();
Bitmap transformed = TransformationUtils.centerCrop(bitmapPool, toTransform, smallerSize, smallerSize);
final Resource<Bitmap> result;
if (toTransform.equals(transformed)) {
result = resource;
} else {
result = BitmapResource.obtain(transformed, bitmapPool);
}
return result;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update("crop transformation".getBytes());
}
});
Glide.with(context)
.load(resourceId)
.apply(transformation)
.into(imageView);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment