Android 使用canvas.DrawText()将自定义字体居中

Android 使用canvas.DrawText()将自定义字体居中,android,xamarin,font-awesome,Android,Xamarin,Font Awesome,我有一个自定义的字体字符串(字体很棒的图标),我想把它画成一个FloatingActionButton。出于这个原因,我必须将字符串转换为可绘制的字符串。 问题是,图标从未居中,即使我尝试以下操作: public override void Draw(global::Android.Graphics.Canvas canvas) { Rect bounds = new Rect(); paint.GetTextBounds(text, 0, text.Length, bounds

我有一个自定义的字体字符串(字体很棒的图标),我想把它画成一个FloatingActionButton。出于这个原因,我必须将字符串转换为可绘制的字符串。 问题是,图标从未居中,即使我尝试以下操作:

public override void Draw(global::Android.Graphics.Canvas canvas)
{
    Rect bounds = new Rect();
    paint.GetTextBounds(text, 0, text.Length, bounds);
    float x = (canvas.Width / 2f) - (bounds.Width() / 2f)
    float y = (canvas.Height / 2f) - (bounds.Height() / 2f);
    canvas.DrawText(text, x, y, paint);
}
结果会是这样的:

public override void Draw(global::Android.Graphics.Canvas canvas)
{
    Rect bounds = new Rect();
    paint.GetTextBounds(text, 0, text.Length, bounds);
    float x = (canvas.Width / 2f) - (bounds.Width() / 2f)
    float y = (canvas.Height / 2f) - (bounds.Height() / 2f);
    canvas.DrawText(text, x, y, paint);
}

我想不出这个问题

提前谢谢

编辑: 如果执行GetBounds,则结果如下:


而不是画布。*使用getBounds()。*不可绘制。getBounds(),也应该是float y=(canvas.Height/2f)+(bounds.Height()/2f);使用加号,而不是减号,也不要使用画布大小,而是可绘制大小您是否更改了float y?因此您的边界为空:bounds.Height()==0,Log.d边界