package com.justwayward.reader.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.AttributeSet; import android.widget.TextView; /** * @author yuyh. * @date 17/1/30. */ public class LetterView extends TextView { // 颜色画板集 private static final int[] colors = { 0xff1abc9c, 0xff16a085, 0xfff1c40f, 0xfff39c12, 0xff2ecc71, 0xff27ae60, 0xffe67e22, 0xffd35400, 0xff3498db, 0xff2980b9, 0xffe74c3c, 0xffc0392b, 0xff9b59b6, 0xff8e44ad, 0xffbdc3c7, 0xff34495e, 0xff2c3e50, 0xff95a5a6, 0xff7f8c8d, 0xffec87bf, 0xffd870ad, 0xfff69785, 0xff9ba37e, 0xffb49255, 0xffb49255 }; private Paint mPaintBackground; private Paint mPaintText; private Rect mRect; private String text; private int charHash; public LetterView(Context context) { this(context, null); } public LetterView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public LetterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); setText(getText().toString()); } private void init() { mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG); mPaintText.setColor(Color.WHITE); mPaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG); mRect = new Rect(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, widthMeasureSpec); // 宽高相同 } @Override protected void onDraw(Canvas canvas) { if (null != text) { // 画圆 canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, mPaintBackground); // 写字 mPaintText.setTextSize(getWidth() / 2); mPaintText.setStrokeWidth(3); mPaintText.getTextBounds(text, 0, 1, mRect); // 垂直居中 Paint.FontMetricsInt fontMetrics = mPaintText.getFontMetricsInt(); int baseline = (getMeasuredHeight() - fontMetrics.bottom - fontMetrics.top) / 2; // 左右居中 mPaintText.setTextAlign(Paint.Align.CENTER); canvas.drawText(text, getWidth() / 2, baseline, mPaintText); } } public void setText(String content) { if (TextUtils.isEmpty(content)) { content = " "; } this.text = String.valueOf(content.toCharArray()[0]); this.text = text.toUpperCase(); charHash = this.text.hashCode(); int color = colors[charHash % colors.length]; mPaintBackground.setColor(color); invalidate(); } }