Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daa233/fcf521ce1c440aa7a4b65f3c25b3ef71 to your computer and use it in GitHub Desktop.
Save daa233/fcf521ce1c440aa7a4b65f3c25b3ef71 to your computer and use it in GitHub Desktop.

自定义View时居中绘制文本需要注意

canvas.drawText(text, x, y, paint)

第一个参数是我们需要绘制的文本,第四个参数是我们的画笔。主要是第二和第三个参数的含义,这两个参数在不同的情况下的值还是不一样的, x默认是这个字符串的左边在屏幕的位置,如果设置了paint.setTextAlign(Paint.Align.CENTER);那就是字符的中心, y是指定这个字符baseline在屏幕上的位置。y不是这个字符中心在屏幕上的位置,而是baseline在屏幕上的位置。

【参考资料】http://blog.csdn.net/lovexieyuan520/article/details/43153275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment