Skip to content

Instantly share code, notes, and snippets.

@virendersran01
Forked from muthuraj57/AvatarView.java
Created July 11, 2020 11:30
Show Gist options
  • Save virendersran01/ec3ca88ceb72cab13ffb98c582cfab69 to your computer and use it in GitHub Desktop.
Save virendersran01/ec3ca88ceb72cab13ffb98c582cfab69 to your computer and use it in GitHub Desktop.
/*
* Initialize fields
* */
protected void init() {
rectF = new RectF();
clipPath = new Path();
imageSize = getResources().getDimensionPixelSize(R.dimen.avatar_size);
cornerRadius = (int) Utils.dpToPixel(2, getResources());
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(16f * getResources().getDisplayMetrics().scaledDensity);
textPaint.setColor(Color.WHITE);
}
/*
* Get the user and set values based on the user
* This is the only exposed api to the developer
* */
public void setUser(User user) {
this.user = user;
setValues();
}
/*
* Set user specific fields in here
* */
private void setValues() {
/*
* User specific color for initial background
* */
paint.setColor(user.getColor());
/*
* Initials of member
* */
text = Helper.getShortName(user.getName());
setDrawable();
if (user.getAvatarUrl() != null) {
Glide.with(getContext())
.load(user.getAvatarUrl())
.placeholder(drawable)
.centerCrop()
.override(imageSize, imageSize)
.into(this);
} else {
setImageDrawable(drawable);
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment