android 4.4上的自定义字体渲染具有空心透明文本(Custom font rendering on android 4.4 has hollow transparent text)

编程入门 行业动态 更新时间:2024-10-25 13:27:30
android 4.4上的自定义字体渲染具有空心透明文本(Custom font rendering on android 4.4 has hollow transparent text)

使用我的一个应用程序在android 4.4上发生了一些奇怪的事情。 我正在使用自定义视图,其中我使用Typeface.createFromAsset设置自定义字体。 如上图所示,渲染在设备/模拟器上正常工作。 当我将自定义视图的内容保存为位图时,会出现此问题:

Bitmap currentBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(currentBitmap); view.draw(canvas);

在android <4.4上一切正常。 保存的图像与屏幕上可见的图像相同。 但是,在4.4我只看到字体边框,文本内部是透明的。 我怎样才能解决这个问题 ?

Something weird is happening on android 4.4 with one of my apps. I am using a custom view in which I set a custom typeface with Typeface.createFromAsset. The rendering works fine on the device/emulator as you can see in the image above. The problem appears when I save the content of the custom View as a bitmap:

Bitmap currentBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(currentBitmap); view.draw(canvas);

Everything works fine on android < 4.4. The saved image looks the same as the one visible on the screen. However, on 4.4 I only see the font borders and the inside of text is transparent. How can I fix this ?

最满意答案

在API 19及更高版本的画布上绘制文本时,您必须注意正在使用的Paint对象的笔触和填充。 在Android的早期版本中,即使使用笔触样式,也始终绘制文本。 在KitKat中,将样式设置为笔划就是这样做的; 抚摸文本而不是填充它。 在渲染文本时,您需要手动设置要填充的绘制样式,然后将其重新设置为笔划以绘制其他内容(如果这是您要执行的操作)。

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mTextPaint.setStyle(Style.STROKE); // do other setup on Paint object // Draw non-text stuff mTextPaint.setStyle(Style.FILL); // do other setup on Paint object // Draw all the text stuff }

When drawing text on a canvas in API 19 and above, you have to be mindful of the stroke and fill for the Paint object that you're using. In prior versions of Android, text was always drawn filled, even when using the stroke style. In KitKat, setting the style to stroke will do exactly that; stroke the text rather than fill it. You will need to manually set the paint style to fill when rendering text and then re-set it to stroke for drawing other things (if that's what you're trying to do).

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mTextPaint.setStyle(Style.STROKE); // do other setup on Paint object // Draw non-text stuff mTextPaint.setStyle(Style.FILL); // do other setup on Paint object // Draw all the text stuff }

更多推荐

本文发布于:2023-07-30 06:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   文本   透明   字体   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!