Android 字体测量高度详解,加下划线、斜线、中线、双中线等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dxf_cs/article/details/80435474

首先,给大家画个图,让大家可以更好的理解。


1.基准线是Baseline 
2.ascent:字体在Baseline上方被推荐的距离(一些字体制作商需要参考这个) 
3.descent:字体在是Baseline下方被推荐的距离(一些字体制作商需要参考这个) 
4.top:ascent的最大值 

5.bottom:descent的最大值

测量字体的高度,我们使用123456   3*bottom,
我们使用ABCD也是3bottom,但是Q例外,Q是3.5*bottom
gj下边界可以到达4*bottom

我们的汉字用4*bottom  混合的就用4*bottom

下面就上个小例子给大家看:

写了一个自定义view

@SuppressLint("AppCompatCustomView")
public class LineView extends TextView {
    private Paint paint = null;
    /**
     * lineMode  来描述线的样式 (种类)
     * 0:表示 没有线 , 1:表示以调横线在底部 , 2:表示一条横线在中间 ,3:表示两条横线在中间 , 4:表示一条斜线
     */
    private int lineMode = 1;
    private int lineColor = 0xff653747;  //线的颜色
    private int lineWidth = 1;  //线宽

    public int getLineMode() {
        return lineMode;
    }

    private void init() {
        paint = new Paint();
        paint.setColor(lineColor);
        paint.setStrokeWidth(lineWidth);
        paint.setAntiAlias(true); //抗锯齿
        paint.setDither(true);  //仿抖动
    }
   public void setLineMode(int lineMode){
        this.lineMode=lineMode;
        invalidate(); //重新绘制,调用这个方法就会触发ondraw重新绘制
   }
    public int getLineWidth() {
        return lineWidth;
    }

    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
        paint.setStrokeWidth(lineWidth);
        invalidate();
    }



    public LineView(Context context) {
        super(context);
        init();
    }

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

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

    @Override
    protected void onDraw(Canvas canvas) {
       // super.onDraw(canvas);
        // textView在绘制文字
        // 自己绘制文字,精准绘制文字
        canvas.drawText(getText()+"",0, (float) (getPaint().getFontMetrics().bottom*4),getPaint());    
       switch (lineMode){
           case 0:
               break;
           case 1:  //底下画一条线
               canvas.drawLine(0,getHeight()-lineWidth,getWidth(),getHeight()-lineWidth,paint);
               break;
           case 2:
               // 线在中间
               canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2,
                       paint);

               break;
           case 3:
           // 画两条线
           canvas.drawLine(0, getHeight() / 3, getWidth(), getHeight() / 3,
                   paint);
           canvas.drawLine(0, getHeight() / 3 * 2, getWidth(),
                   getHeight() / 3 * 2, paint);
           break;
           case 4:  //斜线
               canvas.drawLine(0, 0, getWidth(),
                       getHeight(), paint);
               break;
       }
    }
  //重新测量这个view
    //如果这个view是包裹内容的,我们可以在这里测量出它应该有多大
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        TextPaint textPaint=getPaint();
        float height= (float) (textPaint.getFontMetrics().bottom*4);  //4:就是根据字体是汉字 字母去确定是4还是3.5,3,一般用4
        float width=textPaint.measureText(getText()+"");
        setMeasuredDimension((int)width,(int)height);
    }
}

然后是activity

public class NewActivity extends AppCompatActivity {

    private LineView mLineView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        mLineView = (LineView)findViewById(R.id.lineview);
        mLineView.setLineMode(1); // 1:画底线,2:表示一条横线在中间 ,3:表示两条横线 4,斜线
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.juzihappy.asus.otherexample.newactivity.NewActivity">
  <com.juzihappy.asus.otherexample.newactivity.LineView
      android:id="@+id/lineview"
       android:text="ADX拉萨扩HDJqjglkjHHH" 
      android:textSize="30sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></com.juzihappy.asus.otherexample.newactivity.LineView>
</LinearLayout>
运行一下这个小demo,我相信你会很明白的。


猜你喜欢

转载自blog.csdn.net/dxf_cs/article/details/80435474