【Android】获取要paint的文字的宽高 paint draw text get width

本文详细介绍了如何利用Paintapi自带的函数来获取文本的宽度,并提供了实例代码和API说明,帮助开发者轻松实现文本尺寸的自适应调整。

解决办法:
利用Paint api自带的函数获取:
墙链接
示例:

paint.setColor(textColor);                          //设置画笔颜色
        paint.setTypeface(getTypeface());             //设置字体样式
        paint.setStrokeWidth(8);

        if(TextUtils.isEmpty(TAB))
            TAB = getResources().getString(R.string.fragment_tab);
        //设置画笔字体的大小
        paint.setTextSize((float) (20.0 / 95 * height));
        float textWidth = paint.measureText(TAB);

API说明:

public float measureText (String text)

Added in API level 1
Return the width of the text.

Parameters
text    The text to measure. Cannot be null.
Returns
The width of the text
public float measureText (CharSequence text, int start, int end)

Added in API level 1
Return the width of the text.

Parameters
text    The text to measure
start   The index of the first character to start measuring
end 1 beyond the index of the last character to measure
Returns
The width of the text
public float measureText (String text, int start, int end)

Added in API level 1
Return the width of the text.

Parameters
text    The text to measure. Cannot be null.
start   The index of the first character to start measuring
end 1 beyond the index of the last character to measure
Returns
The width of the text
public float measureText (char[] text, int index, int count)

Added in API level 1
Return the width of the text.

Parameters
text    The text to measure. Cannot be null.
index   The index of the first character to start measuring
count   THe number of characters to measure, beginning with start
Returns
The width of the text

have fun

转载署源-By-KyleCe:
http://blog.youkuaiyun.com/KyleCeshen/article/details/50425317

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值