Skip to content

Instantly share code, notes, and snippets.

@psh
Created August 20, 2016 02:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save psh/68bcf4c21d9ae164e2d16b1420cafad2 to your computer and use it in GitHub Desktop.
Save psh/68bcf4c21d9ae164e2d16b1420cafad2 to your computer and use it in GitHub Desktop.
Glide + Gravatar integration to display a user's Gravatar image in an Android ImageView.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.security.MessageDigest;
import java.util.Locale;
public class GlideGravatarImageLoader {
private static final int IMAGE_SIZE = 96;
private static final String URL_FORMAT = "http://www.gravatar.com/avatar/%1$s.jpg?s=%2$d&d=404&r=g";
public void load(ImageView imageView, String email, Listener listener) {
String hash = convertEmailToHash(email);
Context context = imageView.getContext();
Glide.with(context)
.load(String.format(Locale.getDefault(), URL_FORMAT, hash, IMAGE_SIZE))
.bitmapTransform(new CircularGravatarTransform(Glide.get(context).getBitmapPool()))
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if (listener != null) {
listener.failure();
}
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
if (listener != null) {
listener.success();
}
return false;
}
})
.into(imageView);
}
private String convertEmailToHash(String email) {
final MessageDigest messageDigest;
StringBuilder sb = new StringBuilder();
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
byte[] digest = messageDigest.digest(email.getBytes("UTF-8"));
for (byte b : digest) {
sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
} catch (Exception e) {
sb.setLength(0);
sb.append(email);
}
return sb.toString();
}
private class CircularGravatarTransform implements Transformation<Bitmap> {
private BitmapPool mBitmapPool;
CircularGravatarTransform(BitmapPool pool) {
this.mBitmapPool = pool;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int size = Math.min(source.getWidth(), source.getHeight());
int width = (source.getWidth() - size) / 2;
int height = (source.getHeight() - size) / 2;
Bitmap bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
if (width != 0 || height != 0) {
Matrix matrix = new Matrix();
matrix.setTranslate(-width, -height);
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return BitmapResource.obtain(bitmap, mBitmapPool);
}
@Override
public String getId() {
return "CircularGravatarTransform";
}
}
public interface Listener {
void success();
void failure();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment