fix none avatar issue

This commit is contained in:
houxg
2017-02-16 23:03:36 +08:00
parent efb06e1a59
commit 2d564321b5
3 changed files with 77 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ import com.bumptech.glide.Glide;
import org.houxg.leamonax.R;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.widget.AlphabetDrawable;
import java.util.List;
@@ -71,6 +72,9 @@ public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.AccountH
.centerCrop()
.bitmapTransform(new CropCircleTransformation(holder.avatarIv.getContext()))
.into(holder.avatarIv);
} else {
holder.mAlphabetDrawable.setAlphabet(account.getEmail());
holder.avatarIv.setImageDrawable(holder.mAlphabetDrawable);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
@@ -110,6 +114,8 @@ public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.AccountH
@BindView(R.id.iv_avatar)
ImageView avatarIv;
AlphabetDrawable mAlphabetDrawable = new AlphabetDrawable();
public AccountHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);

View File

@@ -38,6 +38,7 @@ import org.houxg.leamonax.model.User;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.NotebookService;
import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.widget.AlphabetDrawable;
import org.houxg.leamonax.widget.TriangleView;
import butterknife.BindView;
@@ -87,6 +88,7 @@ public class Navigation {
private NotebookAdapter mNotebookAdapter;
private AccountAdapter mAccountAdapter;
private TagAdapter mTagAdapter;
private AlphabetDrawable mAlphabetDrawable = new AlphabetDrawable();
private Mode mCurrentMode = Mode.RECENT_NOTES;
@@ -122,6 +124,7 @@ public class Navigation {
public void onNext(User user) {
AccountService.saveToAccount(user, AccountService.getCurrent().getHost());
refreshUserInfo(AccountService.getCurrent());
mAccountAdapter.notifyDataSetChanged();
}
});
}
@@ -348,6 +351,9 @@ public class Navigation {
.centerCrop()
.bitmapTransform(new CropCircleTransformation(mActivity))
.into(mAvatarIv);
} else {
mAlphabetDrawable.setAlphabet(account.getEmail());
mAvatarIv.setImageDrawable(mAlphabetDrawable);
}
}

View File

@@ -0,0 +1,65 @@
package org.houxg.leamonax.widget;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import org.houxg.leamonax.Leamonax;
import org.houxg.leamonax.R;
public class AlphabetDrawable extends Drawable {
private Paint mPaint;
private int mBackgroundColor = Color.BLUE;
private String mAlphabet;
public AlphabetDrawable() {
mPaint = new Paint();
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextAlign(Paint.Align.CENTER);
mBackgroundColor = Leamonax.getContext().getResources().getColor(R.color.colorAccent);
}
@Override
public void draw(Canvas canvas) {
Rect bound = getBounds();
float rad = Math.min(bound.width(), bound.height()) / 2.0f;
mPaint.setColor(mBackgroundColor);
canvas.drawCircle(bound.centerX(), bound.centerY(), rad, mPaint);
if (!TextUtils.isEmpty(mAlphabet)) {
mPaint.setColor(Color.WHITE);
float textSize = bound.height() * 0.6f;
mPaint.setTextSize(textSize);
Paint.FontMetrics metrics = mPaint.getFontMetrics();
float baseline = (bound.bottom + bound.top - metrics.bottom - metrics.top) / 2;
canvas.drawText(mAlphabet, bound.centerX(), baseline, mPaint);
}
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
public void setAlphabet(String alphabet) {
if (!TextUtils.isEmpty(alphabet)) {
mAlphabet = alphabet.substring(0, 1);
}
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}