第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android Paint:.measureText()vs .getTextBounds()

Android Paint:.measureText()vs .getTextBounds()

陪伴而非守候 2019-09-18 14:48:08
我正在測量文本Paint.getTextBounds(),因為我有興趣獲得要渲染的文本的高度和寬度。但是,實際呈現(xiàn)的文本總是比填充.width()的Rect信息寬一些getTextBounds()。令我驚訝的是,我進行了測試.measureText(),發(fā)現(xiàn)它返回了一個不同的(更高)值。我試一試,發(fā)現(xiàn)它是正確的。為什么他們報告不同的寬度?我怎樣才能正確獲得高度和寬度?我的意思是,我可以使用.measureText(),但后來我不知道我是否應(yīng)該相信.height()返回者getTextBounds()。根據(jù)要求,這里是重現(xiàn)問題的最小代碼:final String someText = "Hello. I believe I'm some text!";Paint p = new Paint();Rect bounds = new Rect();for (float f = 10; f < 40; f += 1f) {    p.setTextSize(f);    p.getTextBounds(someText, 0, someText.length(), bounds);    Log.d("Test", String.format(        "Size %f, measureText %f, getTextBounds %d",        f,        p.measureText(someText),        bounds.width())    );}輸出顯示差異不僅大于1(并且沒有最后一刻的舍入誤差),而且似乎隨著大小而增加(我即將得出更多結(jié)論,但它可能完全取決于字體):D/Test    (  607): Size 10.000000, measureText 135.000000, getTextBounds 134D/Test    (  607): Size 11.000000, measureText 149.000000, getTextBounds 148D/Test    (  607): Size 12.000000, measureText 156.000000, getTextBounds 155D/Test    (  607): Size 13.000000, measureText 171.000000, getTextBounds 169D/Test    (  607): Size 14.000000, measureText 195.000000, getTextBounds 193D/Test    (  607): Size 15.000000, measureText 201.000000, getTextBounds 199D/Test    (  607): Size 16.000000, measureText 211.000000, getTextBounds 210D/Test    (  607): Size 17.000000, measureText 225.000000, getTextBounds 223D/Test    (  607): Size 18.000000, measureText 245.000000, getTextBounds 243D/Test    (  607): Size 19.000000, measureText 251.000000, getTextBounds 249
查看完整描述

3 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗 獲得超6個贊

我的經(jīng)驗是,getTextBounds將返回封裝文本的絕對最小邊界矩形,而不一定是渲染時使用的測量寬度。我還想說measureText假設(shè)一行。


為了獲得準(zhǔn)確的測量結(jié)果,您應(yīng)該使用它StaticLayout來渲染文本并拉出測量結(jié)果。


例如:


String text = "text";

TextPaint textPaint = textView.getPaint();

int boundedWidth = 1000;


StaticLayout layout = new StaticLayout(text, textPaint, boundedWidth , Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

int height = layout.getHeight();


查看完整回答
反對 回復(fù) 2019-09-18
?
慕田峪7331174

TA貢獻(xiàn)1828條經(jīng)驗 獲得超13個贊

這里是對真正問題的描述:


簡單的簡單回答是沒有開始的Paint.getTextBounds(String text, int start, int end, Rect bounds)回報。也就是說,要獲得將通過使用getTextBounds()中的相同 Paint對象調(diào)用而設(shè)置的文本的實際寬度,您應(yīng)該添加Rect的左側(cè)位置。像這樣的東西:Rect(0,0)Canvas.drawText(String text, float x, float y, Paint paint)


public int getTextWidth(String text, Paint paint) {

    Rect bounds = new Rect();

    paint.getTextBounds(text, 0, end, bounds);

    int width = bounds.left + bounds.width();

    return width;

}

注意這一點bounds.left- 這是問題的關(guān)鍵。


通過這種方式,您將獲得與使用時相同的文本寬度Canvas.drawText()。


并且相同的功能應(yīng)該是獲取height文本:


public int getTextHeight(String text, Paint paint) {

    Rect bounds = new Rect();

    paint.getTextBounds(text, 0, end, bounds);

    int height = bounds.bottom + bounds.height();

    return height;

}

Ps:我沒有測試這個確切的代碼,但測試了這個概念。


查看完整回答
反對 回復(fù) 2019-09-18
  • 3 回答
  • 0 關(guān)注
  • 864 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號