android开发分享测量在canvas上绘制的文字宽度(Android)

有没有一种方法可以根据用于绘制的Paint使用drawText()方法返回在Androidcanvas上绘制文本的宽度(以像素为单位)?

    你有没有看过android.graphics.Paint.measureText(String txt) ?

    Paint paint = new Paint(); Rect bounds = new Rect(); int text_height = 0; int text_width = 0; paint.setTypeface(Typeface.DEFAULT);// your preference here paint.setTextSize(25);// have this the same as your text size String text = "Some random text"; paint.getTextBounds(text, 0, text.length(), bounds); text_height = bounds.height(); text_width = bounds.width(); 

    补充答案

    Paint.measureTextPaint.getTextBounds返回的宽度有一些细微的差别。 measureText返回一个宽度,该宽度包括填充string开始和结尾的字形的advanceX值。 由getTextBounds返回的Rect宽度不具有此填充,因为边界是紧密缠绕文本的Rect

    资源

    那么我做了不同的方式:

     String finalVal ="Hiren Patel"; Paint paint = new Paint(); paint.setTextSize(40); Typeface typeface = Typeface.createFromAsset(getAssets(), "Helvetica.ttf"); paint.setTypeface(typeface); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.FILL); Rect result = new Rect(); paint.getTextBounds(finalVal, 0, finalVal.length(), result); Log.i("Text dimensions", "Width: "+result.width()+"-Height: "+result.height()); 

    希望这会帮助你。

    我使用方法measureText()和getTextPath()+ computeBounds(),并build立一个Excel的所有文本属性的固定大小的字体,可以在 /master/TextWidth.xlsx 。 在那里你会发现其他文本属性,如提升等简单的公式

    应用程序以及函数drawFontTest()用于生成在Excel中使用的原始值也可在此回购。

    你可以使用“textPaint.getTextSize()”来获取文本宽度

      以上就是android开发分享测量在canvas上绘制的文字宽度(Android)相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/516709.html

      (0)
      上一篇 2020年12月3日
      下一篇 2020年12月3日

      精彩推荐