android中利用Paint繪製文字使其居中

2021-07-23 13:07:18 字數 501 閱讀 5925

要使文字垂直居中,可利用canvas的drawtext(text, x, y, paint)方法,其中第三個引數y應該是baseline的值,計算公式如下:

int baseline = (getmeasuredheight() - (paint.descent() - paint.ascent())) / 2 - paint.ascent();
即使得ascent和descent到繪製邊界上下兩邊界的距離一致,這個距離再加上ascent()的絕對值,即 -paint.ascent(),即為baseline的值。

水平居中的話,上述drawtext方法的x的值可以設定為:

int x = (getmeasuredwidth() - textwidth) / 2;
其中textwidth為繪製文字的寬度,可以利用
int textwidth = (int) paint.measuretext(text);
進行計算。

android利用paint 實現手勢解鎖

1 自定義view 2 mycycle類對座標進行計算 package com.android.view public class mycycle public void setr float r public boolean isontouch public void setontouch boo...

android中Paint類引數介紹

paint即畫筆,在繪製文字和圖形用它來設定圖形顏色,樣式等繪製資訊。1.圖形繪製 setargb int a,int r,int g,int b 設定繪製的顏色,a代表透明度,r,g,b代表顏色值。setalpha int a 設定繪製圖形的透明度。setcolor int color 設定繪製的...

Android中Paint字型屬性的設定

在android sdk中使用typeface類來定義字型,可以通過常用字型型別名稱進行設定,如設定預設黑體 paint mp new paint mp.settypeface typeface.default bold 常用的字型型別名稱還有 除了字型型別設定之外,還可以為字型型別設定字型風格,如...