Android custom controls (5) --- actual combat articles (optimization articles)

In the previous article, we found that we have succeeded, but the text is too close to the four sides. However, we found that adding padding to the control in the layout file did not work. If it doesn't work, you can try it.

This is because we didn't write logic in the code, hahaha, don't talk nonsense, just upload the code! ! !

package com.example.mytextview;

//import javax.swing.text.View;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class mTextView extends View {
    //1、设置自定义属性变量
    private int mTextSize = 16;
    private int mTextColor = Color.RED;
    private String mText;

    //设置文字画笔
    private Paint textPaint;

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

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

    public mTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //2、获取装有自定义属性值的数值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.mTextView);
        //3、精确获取自定义属性值
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.mTextView_mTextSize,mTextSize);//源码用的这个方法,参照 TextView
        mTextColor = typedArray.getColor(R.styleable.mTextView_mTextColor,mTextColor);
        mText = typedArray.getString(R.styleable.mTextView_mText);
        //4、回收 typedArray
        typedArray.recycle();

        initData();
    }

    private void initData() {
        textPaint = new Paint();
        //抗锯齿
        textPaint.setAntiAlias(true);
        //设置颜色
        textPaint.setColor(mTextColor);
        //设置字体大小
        textPaint.setTextSize(mTextSize);
    }

    //5、测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        //获取模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //判断 如果是 wrap_content 模式,则 布局宽高 与 字体大小和字体长度有关
        if(widthMode == MeasureSpec.AT_MOST){
            //设置文字边界
            Rect bounds = new Rect();
            //获取文字边界
            textPaint.getTextBounds(mText,0,mText.length(),bounds);
            //获取边界宽度  ===  获取文字宽度
            width = bounds.width();
        }
        if (heightMode == MeasureSpec.AT_MOST){
            //设置文字边界
            Rect bounds = new Rect();
            //获取文字边界
            textPaint.getTextBounds(mText,0,mText.length(),bounds);
            //获取边界高度  ===  获取文字高度
            height = bounds.height();
        }
        //最后设置宽高
        setMeasuredDimension(width + getPaddingLeft() + getPaddingRight(),height + getPaddingBottom() + getPaddingTop());  、、、、修改的地方
    }

    //6、绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //drawText 中存在四个参数分别是: 要显示的文本,基线x方向的起始点,基线y方向的起始点,画笔
        Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt();
        //botto 为正值 top 为负值(看图中画的坐标系)
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
        int baseLine = getHeight()/2 + dy;

        float dx = getPaddingLeft();  、、、、修改的地方
        canvas.drawText(mText,dx,baseLine,textPaint);    、、、、修改的地方
    }
}

Effect picture:

Perfect ending, hahaha

Guess you like

Origin blog.csdn.net/qq_41885673/article/details/114107686