package com.ez.ertc.call.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.ez.ertc.utils.ViewUtil;

/**
 *
 *
 * @author xiezuoyuan
 * @time 2021/3/4 14:22
 */
public class WaterMarkView extends View  {
    private String mNickname,mPhone;
    private int mSize;
    private TextPaint mTextPaint;
    public WaterMarkView(Context context) {
        super(context);
        init();
    }

    public WaterMarkView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaterMarkView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mSize = ViewUtil.INSTANCE.dp2px(getContext(),12f);
        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔
        mTextPaint.setColor(0x66333333);//采用的颜色
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    public void setWaterMarkSize(int size){
        mSize = size;
    }
    public void setWaterMark(String nickname,String phone){
        mNickname = nickname;
        mPhone = phone;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!TextUtils.isEmpty(mNickname)){
            mTextPaint.setTextSize(mSize);//字体大小
            int x = getMeasuredWidth() / 2;
            int y = getMeasuredHeight() / 2;
            canvas.rotate(-45,x,y);
            canvas.drawText(mNickname, x, y, mTextPaint);
            if (!TextUtils.isEmpty(mPhone)){
                //取到一行字的高度，mPhone下移一个高度的位置
                Rect bounds = new Rect();
                mTextPaint.getTextBounds(mNickname, 0, mNickname.length(), bounds);
                canvas.drawText(mPhone, x, y+bounds.height()+10, mTextPaint);
            }
        }
    }
}
