canvas.drawTextOnPath()无效
移动开发
2018-05-11 15:48:56
阅读次数: 1
转载自:http://bbs.9ria.com/thread-230883-1-1.html
为了实现在透明圆环上绘制字体,查到是用canvas.drawTextOnPath()这个函数。但遗憾的是,我在手机上死活都画不出来字体。用canvas.drawPath()正常,说明path是正确的,但就是drawTextOnPath()无效。在网上搜了一堆关于canvas画图的东西,链接如下:
android截取图片一个圆,得到的图片总是存在锯齿,怎么办?
android Path、Typeface和绘制弧线
setTypeface方法:设置字体样式
canvas.drawText看不到 字体
Canvas、Path 和 Paint 实例 (游戏开发必备)
Android 中 drawTextOnPath 文字效果演示(中级)
图形与图形处理(Path、drawTextOnPath) 怎么实现字符串 90度旋转,使用canvas.drawText()
android中使用canvas.drawText函数可否设置旋转
上面这些东西足以把canvas绘制的方方面面讲的很清除了,但我的canvas.drawTextOnPath()就是不中。后来用google查的国外盆有的一篇文章:
drawTextOnPath() not working from 4.1.2 to 4.0.3
里面给出的解释是:
Answer
drawTextOnPath() was not supported with hardware acceleration until Android 4.1. To work around this problem, simply set a software layer type on your View when running on Android < 4.1. Just call View.setLayerType(View.LAYER_TYPE_SOFTWARE, null). This will force software rendering and fix your problem.
原来这是android SDK的一个bug,在android4.0.3及其以下(其实是4.1版本 以下),drawTextOnPath是无效的,解决方法是在自定义ImageView的构造函数里增加这一句话: this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
一切就OK了!
另外还可以在Manifinest里面禁止硬件加速参见http://bbs.csdn.net/topics/380198530。但我不推荐这种方式,这会使所有的view包括程序中的surfaceView都禁止加速了,损失太大。
转载自jianweicao.iteye.com/blog/2205100