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

解决办法:
利用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.csdn.net/KyleCeshen/article/details/50425317

你可能感兴趣的:(Android问题记录)