自定义控件-绘制文字-drawText
如何保证自己的绘制的文字居中?
`// 第一类 根据坐标
public void drawText (String text, float x, float y, Paint paint)
public void drawText (String text, int start, int end, float x, float y, Paint paint)
public void drawText (CharSequence text, int start, int end, float x, float y, Paint paint)
public void drawText (char[] text, int index, int count, float x, float y, Paint paint)
// 第二类 drawPosText ()是根据一个个坐标点指定文字位置
public void drawPosText (String text, float[] pos, Paint paint)
public void drawPosText (char[] text, int index, int count, float[] pos, Paint paint)
// 第三类 drawTextOnPath ()是根据路径绘制
public void drawTextOnPath (String text, Path path, float hOffset, float vOffset, Paint paint)
public void drawTextOnPath (char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)`
想要文字居中,重点就是要找到控件的中心点。
如图:
具体代码:
package com.example.vitomvp.ui;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
/**
* 必须要在设置了画笔Paint的文字大小,宽度等属性,
* 才能获取FontMetrics
* Paint.FontMetrics fontMetrics=paint.getFontMetrics();
* fontMetrics.top
* fontMetrics.ascent
* fontMetrics.descent
* fontMetrics.bottom
*/
public class TwwView extends View {
public TwwView(Context context) {
this(context,null);
}
public TwwView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public TwwView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//背景框
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setStyle(Paint.Style.FILL);
RectF rectF = new RectF(200,200,800,600);//逆时针
canvas.drawRect(rectF,paint);
Paint textPaint = new Paint();
textPaint.setColor(Color.BLUE);
textPaint.setStrokeWidth(5);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(50);
textPaint.setTextAlign(Paint.Align.CENTER);
String string ="base Text";
//计算基线baseLine 文字基线类似于一个坐标0点
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
Log.d("tww","fontMetrics.bottom:"+fontMetrics.bottom);
Log.d("tww","fontMetrics.top:"+fontMetrics.top);
Log.d("tww","fontMetrics.ascent:"+fontMetrics.ascent);
Log.d("tww","fontMetrics.descent:"+fontMetrics.descent);
/*
* 距离 = 文字高度的一半 - 基线到文字底部的距离(也就是bottom)
= (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom
* */
float distance = (fontMetrics.bottom-fontMetrics.top)/2-fontMetrics.bottom;
//文字的y点=就是矩形的中心Y值+矩形中心到基线的距离
float baseLine = rectF.centerY()+distance;
canvas.drawText(string,rectF.centerX(),baseLine,textPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Rect bounds = new Rect();
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.AT_MOST) {//是wrap_content的宽高处理
//计算文字的宽度 与文字的size和font有关
//测量文字myText 起始点为x==0 终点到text的末尾 计算完成之后 rect会被赋值
mPaint.getTextBounds(mString, 0, mString.length(), bounds);
width = bounds.width() + getPaddingStart() + getPaddingEnd();
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode == MeasureSpec.AT_MOST) {
mPaint.getTextBounds(mString, 0, mString.length(), bounds);
height = bounds.height() + getPaddingTop() + getPaddingBottom();
}
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint.FontMetricsInt fm = mPaint.getFontMetricsInt();
Log.e("tww","fm.descent"+fm.descent);
Log.e("tww", "fm.ascent " + fm.ascent);
Log.e("tww","fm.bottom"+fm.bottom);
Log.e("tww","fm.top"+fm.top);
int baseLine = getHeight() / 2 - fm.descent + (fm.bottom - fm.top) / 2;
int baseLine2 = getHeight() / 2 - fm.bottom / 2 - fm.top / 2;
// x =getPaddingStart() ,y = baseLine
//drawText(text, x, y, paint);
canvas.drawText(mString, getPaddingStart(), baseLine2, mPaint);
}
private int sp2px(int sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
}
private int dip2px(int dip) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
}
参考来自:https://www.jianshu.com/p/8b97627b21c4
来源:简书-biloba
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!