环信圆形头像实现(glide圆形头像)

环信SDK中的聊天界面,iOS使用的是圆形头像,android使用的方形头像,这两者UI都不统一,没办法,只能将android的方形头像改成圆形头像(因为方形头像有种遗像的赶脚)。最初使用CircleImage和环信的聊天界面相结合的方法,使用github上的CircleImageView,app老是崩溃,后来使用自己项目中的CircleImageView,可以出现效果,但是滑动中会出现头像残留在标题栏中或者输入框中的情况,怀疑过listview中的tag导致,如果自己手动加tag,则会和glide框架有冲突。最后采用了transform完美解决这个问题。

环信圆形头像实现(glide圆形头像)_第1张图片


public class GlideCircleTransform extends BitmapTransformation {

public GlideCircleTransform(Context context) {

super(context);

}

@Override

protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {

return circleCrop(pool, toTransform);

}

private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {

if (source == null) return null;

int size = Math.min(source.getWidth(), source.getHeight());

int x = (source.getWidth() - size) / 2;

int y = (source.getHeight() - size) / 2 ;


// TODO this could be acquired from the pool too

Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);

if (result == null) {

result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

}

Canvas canvas = new Canvas(result);

Paint paint = new Paint();

paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

paint.setAntiAlias(true);

float r = size / 2f;

canvas.drawCircle(r, r, r, paint);

return result;

}

@Override

public String getId() {

return getClass().getName();

}

}

使用transform的方法,在需要自定义圆形头像的位置时,需要删除app看头像位置,因为glide存在缓存,如果不想删除app,请使用glide的diskCacheStrategy(DiskCacheStrategy.NONE)属性

你可能感兴趣的:(环信圆形头像实现(glide圆形头像))