Android 初阶自定义 View 字符头像
2016-10-24 00:10 阅读(261)
public class CharAvatarView extends ImageView {
    private static final String TAG = CharAvatarView.class.getSimpleName();
    // 颜色画板集
    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, 0xffa94136
    };

    private Paint mPaintBackground;
    private Paint mPaintText;
    private Rect mRect;

    private String text;

    private int charHash;

    public CharAvatarView(Context context) {
        this(context, null);
    }

    public CharAvatarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CharAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRect = new Rect();
    }
}
public CharAvatarView(Context context) {
    super(context);
}
public CharAvatarView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public CharAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

第一种属于程序内实例化时采用,之传入 Context 即可

CharAvatarView avatarView = new CharAvatarView(this);


public CharAvatarView(Context context) {
      this(context, null);
  }

  public CharAvatarView(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
  }

  public CharAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      init();
  }

  private void init() {
      mPaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
      mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
      mRect = new Rect();
  }
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); // 宽高相同
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (null != text) {
        int color = colors[charHash % colors.length];
        // 画圆
        mPaintBackground.setColor(color);
        canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, mPaintBackground);
        // 写字
        mPaintText.setColor(Color.WHITE);
        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);
    }
}
  1. 首先从颜色数组里根据 hash 取余得到背景颜色

  2. 然后画出背景圆

  3. 接下来就是写字

  4. 最后是对字居中的处理

/**
 * @param content 传入字符内容
 * 只会取内容的第一个字符,如果是字母转换成大写
 */
public void setText(String content) {
    if (content == null) {
        content=" ";
    }
    this.text = String.valueOf(content.toCharArray()[0]);
    this.text = text.toUpperCase();
    charHash = this.text.hashCode();
    // 重绘
    invalidate();
}
compile 'com.github.xcc3641:charavatarview:0.1'
<com.hugo.charavatarview.CharAvatarView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/avatar"/>
CharAvatarView mAvatarView;
mAvatarView = (CharAvatarView) findViewById(R.id.avatar);
mAvatarView.setText("谢三弟");