工作日志记录:关于脉脉这款应用的默认用户头像的一种实现方法


工作日志记录:脉脉这款应用中默认用户头像设计的很有新意,当用户第一次注册时它会根据用户的昵称的第一个字作为头像中的主要文字,思考了一下我用自己的方式将他实现出来,可能以后会借鉴这种设计方式。


效果图:


实现代码不多,如下:

package testedittext.xuganwen.com.testedittext;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Random;

/**
 * Created by Administrator on 2017/11/15 0015.
 */

public class MyImageVIew extends ImageView {

    private final Paint paint;
    private final Paint paint2;

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

    public MyImageVIew(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyImageVIew(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(1f);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        paint2 = new Paint();
        paint2.setColor(Color.WHITE);
        paint2.setStrokeWidth(1f);
        paint2.setAntiAlias(true);
        paint2.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,paint);
        paint2.setTextAlign(Paint.Align.CENTER);
//        paint2.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
        paint2.setTextSize(getWidth()/2);
        Rect rect=new Rect();
        paint2.getTextBounds(text,0,1,rect);
        Paint.FontMetricsInt fm = paint2.getFontMetricsInt();
        int baseLineY = getHeight()/2 +(fm.bottom - fm.top)/2 - fm.bottom;
        canvas.drawText(text,getWidth()/2,baseLineY,paint2);
    }

    String text="测";
    public void setText(String text){
        this.text=text;
        float random1 = (float) Math.random() * 255;
        float random2 = (float) Math.random() * 255;
        float random3 = (float) Math.random() * 255;
        int a = Math.round(random1);
        String a1=Integer.toHexString(a);
        int b=Math.round(random2);
        String  b1=Integer.toHexString(b);
        int c=Math.round(random3);
        String  c1=Integer.toHexString(c);
        if(a<16){
           a1=""+0+Integer.toHexString(a);
        }
        if(b<16){
             b1=""+0+Integer.toHexString(b);
        }
        if(c<16){
            c1=""+0+Integer.toHexString(c);
        }
        Log.i("yanse",""+a+b+c);
        paint.setColor(Color.parseColor("#"+a1+b1+c1));
        invalidate();
    }
}


MainActivity.java代码如下:

package testedittext.xuganwen.com.testedittext;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText edit;
    private MyImageVIew iv;
    private Button pop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        pop=(Button)findViewById(R.id.pop);
        edit=(EditText)findViewById(R.id.edit);
        iv=(MyImageVIew)findViewById(R.id.iv);


    }

    @Override
    protected void onResume() {
        super.onResume();

//        pop.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                showSoftInputFromWindow(MainActivity.this,edit,false);
//            }
//        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if(charSequence.length()!=0) {
                        iv.setText(charSequence.toString().substring(charSequence.length() - 1));
                    }else {
                        charSequence="测";
                        iv.setText(charSequence.toString().substring(charSequence.length() - 1));
                    }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }


    /**
     * EditText获取焦点并显示软键盘
     */
    public  void showSoftInputFromWindow(Activity activity, EditText editText, boolean hideOrNot) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
//        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //打开软键盘
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(hideOrNot){
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }else{
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}


不足之处:这个自定义控件中的代码有一个不足之处,就是十六进制颜色转换我觉得比较呆板甚至是很蠢,不知道各位大佬有什么好的实现方案或者是方法的时候,希望能不吝在评论区指教一下,在此多谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值