Commit b0497388 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix user avatar blink.

parent ad861a84
package chat.rocket.android.helper; package chat.rocket.android.helper;
import android.content.Context;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.widget.ImageView; import android.widget.ImageView;
import com.amulyakhare.textdrawable.TextDrawable; import com.amulyakhare.textdrawable.TextDrawable;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
...@@ -64,27 +65,29 @@ public class Avatar { ...@@ -64,27 +65,29 @@ public class Avatar {
* render avatar into imageView. * render avatar into imageView.
*/ */
public void into(final ImageView imageView) { public void into(final ImageView imageView) {
Picasso.with(imageView.getContext()) Object tag = imageView.getTag();
.load(getImageUrl()) if (tag != null && tag instanceof String) {
.into(imageView, new Callback() { String username = (String) tag;
@Override public void onSuccess() { if (this.username.equals(username)) {
return;
} }
@Override public void onError() {
textInto(imageView);
} }
}); imageView.setTag(this.username);
final Context context = imageView.getContext();
Picasso.with(context)
.load(getImageUrl())
.placeholder(getTextDrawable(context))
.into(imageView);
} }
private void textInto(final ImageView imageView) { private Drawable getTextDrawable(Context context) {
int round = (int) (4 * imageView.getContext().getResources().getDisplayMetrics().density); int round = (int) (4 * context.getResources().getDisplayMetrics().density);
imageView.setImageDrawable(TextDrawable.builder() return TextDrawable.builder()
.beginConfig() .beginConfig()
.useFont(Typeface.SANS_SERIF) .useFont(Typeface.SANS_SERIF)
.endConfig() .endConfig()
.buildRoundRect(getInitialsForUser(username), getColorForUser(username), round)); .buildRoundRect(getInitialsForUser(username), getColorForUser(username), round);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment