美文网首页Android
Android 仿华为手机通讯录头像显示(名称首字符或者是末尾字

Android 仿华为手机通讯录头像显示(名称首字符或者是末尾字

作者: 148112b653f3 | 来源:发表于2018-08-08 20:46 被阅读0次
玩笑时间

产品经理:根据手机壳颜色个给我切换主题色
开发:实现不了(你咋不根据内裤颜色变色勒)
……开干中


正题在这

如华为手机通讯录,使用好友的名称首字符或者是末尾字符单配随机颜色背景作为好友头像,在UI显示上避免了使用去全部使用默认头像的尴尬(除了美观界面,也没啥作用了哈)

1.支持头像颜色自定义,可以自选颜色作为随机颜色

2.支持字符名称首字符或者末尾字符显示

3.支持一句话引用(文末附有giuhub地址,点个赞哦,亲)

S80808-201704.jpg
S80808-201736.jpg
 public class CharPortraitView extends android.support.v7.widget.AppCompatTextView {
//是否随机颜色
private boolean isRandom = false;
//随机类
private Random random;
//背景颜色
private int mBackColor;
//上下文
private Context mContext;
//随机背景颜色数组
private String[] colors;
//显示首字符
private boolean mHead=true;
//文本内容
private String mContent;


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

public CharPortraitView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    init(attrs);
    build();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int minSize = Math.min(width, height);
    setMeasuredDimension(minSize, minSize);
}

/**
 * 初始化设置
 */
private void init(AttributeSet attrs) {
    // 初始化随机数
    random = new Random();
    // 获取参数
    TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.CharPortraitView);
    // 获取是否随机背景
    isRandom = array.getBoolean(R.styleable.CharPortraitView_random, false);
    // 获取背景颜色
    mBackColor = array.getColor(R.styleable.CharPortraitView_back_color, Color.BLUE);
    array.recycle();

}

//设置
private void build() {
    if (!isRandom) {
        mBackColor = getRandomColor(); //产生随机颜色
    }
    // 设置居中
    setGravity(Gravity.CENTER);
    // 设置自适应文字大小
    TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
    // 设置背景颜色
    setBackgroundResource(R.drawable.shape_drawable);
    GradientDrawable drawable = (GradientDrawable) getBackground();
    drawable.setColor(mBackColor);
    setBackgroundDrawable(drawable);
    if (mContent==null){
        return;
    }
    if (mHead){
        setText(mContent.substring(0, 1));//取首字符显示
    }else {
        setText(mContent.substring(mContent.length()-1, mContent.length()));//取末尾字符显示
    }
}

/**
 * 设置文本内容
 *@param mHead  true 第一个字符 false 最后一个字符
 * @return
 */
public CharPortraitView setHead(boolean mHead) {
    this.mHead=mHead;
    build();
    return this;
}
//设置文本内容
public CharPortraitView setContent(String str) {
    this.mContent=str;
    build();
    return this;
}

//设置背景颜色
public CharPortraitView setBackColor(int backColor) {
    mBackColor = backColor;
    isRandom = false;
    build();
    return this;
}

//设置是否开启随机颜色
public CharPortraitView setRandom(boolean isRandom) {
    this.isRandom = isRandom;
    build();
    return this;
}

/**
 * 设置随机背景颜色数组
 * @param colors
 * @return
 */
public CharPortraitView setBackColor(String[] colors){
    this.colors=colors;
    build();
    return this;
}

/**
 * 获取随机背景颜色
 * @return
 */
private int getRandomColor() {
    String[] colorArray;
    if (colors!=null&&colors.length>0){
       colorArray=colors;
    }else {
        colorArray=mContext.getResources().getStringArray(R.array.color);
    }
    int value = random.nextInt(colorArray.length);
    return Color.parseColor(colorArray[value]);
}


}

Github地址 https://github.com/Maolaos/CharPortraitView

相关文章

网友评论

    本文标题:Android 仿华为手机通讯录头像显示(名称首字符或者是末尾字

    本文链接:https://www.haomeiwen.com/subject/xptfbftx.html