簡體   English   中英

Android工作室canvas獲取中心

[英]Android studio canvas get center

看我是 android 的新手,但有一些 java 知識

我學到

在 canvas 中繪制文本

獲得寬度

to get height but how to put the text in the centre of canvas no matter how big the device, the canvas is I want to do this in java without XML change.

嘗試這個:

 Paint text = new Paint();
 text.setTextAlign(Paint.Align.CENTER);

 int xPosition = (canvas.getWidth() / 2);
 int yPosition = (int) ((canvas.getHeight() / 2) - ((text.descent() + text.ascent()) / 2)) ; 


 canvas.drawText("Hello World", xPosition, yPosition, text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM