栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

android canvas drawText从宽度设置字体大小?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android canvas drawText从宽度设置字体大小?

这是一种更有效的方法:

private static void setTextSizeForWidth(Paint paint, float desiredWidth,        String text) {    // Pick a reasonably large value for the test. Larger values produce    // more accurate results, but may cause problems with hardware    // acceleration. But there are workarounds for that, too; refer to    // http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache    final float testTextSize = 48f;    // Get the bounds of the text, using our testTextSize.    paint.setTextSize(testTextSize);    Rect bounds = new Rect();    paint.getTextBounds(text, 0, text.length(), bounds);    // Calculate the desired size as a proportion of our testTextSize.    float desiredTextSize = testTextSize * desiredWidth / bounds.width();    // Set the paint for that size.    paint.setTextSize(desiredTextSize);}

然后,您所要做的就是

setTextSizeForWidth(paint, 400, str);
(400为问题中的示例宽度)。

为了获得更高的效率,您可以使它

Rect
成为静态类成员,从而避免每次实例化它。但是,这可能会引入并发问题,并且可能会阻碍代码的清晰度。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号