如何使文本位图和绘制 [英] How to Render text to bitmap and draw it

查看:198
本文介绍了如何使文本位图和绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和我的团队目前正在创建使用GlSurfaceView使用OpenGL ES 2.0的一个图形应用程序。

我们最近已显示的网格和刻度线的曲线图,现在我已分配给实现数字刻度的任务和标记x和y轴为X和Y。

在这里输入的形象描述

做了很多的研究后,我已经确定通过渲染字符的字符串为位图来完成这个任务。我曾经遇到过在实现这一目标的诸多问题。我理解的基本概念。我知道我将需要的字母数字字符0123456789和XY和 - (为-x和-y刻度)。我见过很多不同的例子,并已广泛试图在这里跟随JVitella的例子这里

我开始掌握这个概念,但至于我的字符串去我知道我有13个字符,所以我的位图应该多大?

此外,在Jvitelas例如我的code傻眼了:

 可绘制背景= context.getResources()getDrawable(R.drawable.background)。

我不明白究竟是怎么回事,当我code这个我收到的上下文中的语法错误。

有关我的应用我明白我需要将字符串保存到一个位图喜欢这个。我将创建一个位图,但它应该多大?然后,我创建从位图和canvas.drawText成位图的画布。

  [0 1 2 3 4]
  | 5 6 7 8 9 |
  [X Y Z]

基本上我问:


  • 如何实现上述以下位图?

  • 如何将我从位图绘制的个位数?

  • 如何将我得出的数字与一个以上的数字?


解决方案

你问了很多问题,但我会尽力回答几个:



  •   

    因此​​,如何大应我的位图是什么?


    这真的取决于你,取决于你想要的清晰文字是。你可以分配一个巨大的位图与数百个像素的每个字符将缩小得非常好,还是一个非常小的位图分辨率有限。我会说什么字体大小你想拥有,高度至少分配,很多像素每个字符。所以,如果你想画的东西与20的字体大小,那么也许你用3×20或100x60需要一个位图5X20。



  •   

    我如何从位图绘制的个位数?


    您会在你想画一个字母的地方画一个四OpenGL的,并且您使用四边形的纹理坐标挑的信。

    例如,如果我想画一个X,那么你在屏幕上绘制一个四,并为它分配的texcoords从(0,0)到(0.2,0.33),它选择质地的左1/5,和纹理的底部1/3。您将看到如何一箱这样的线与在纹理的X的位置。



  •   

    我怎么会得出的数字与一个以上的数字?


    您只画两个独立的个位数旁边给对方。



如果您在这里唯一的目标就是在Android中绘制文本,它可能更容易只需要使用的FrameLayout ,以及层 TextViews 凌驾你的 GLSurfaceView 。 OpenGL的是不适合的文本,这使得它有点麻烦。

I am new to android, and my group is currently creating a graphing application using a GlSurfaceView using opengl es 2.0.

We have recently displayed the grid and tickmarks on the plot and now I have been assigned the task to implement a numeric scale and labeling the x and y axis as "X" and "Y".

After doing a lot of research I have determined to accomplish this by rendering a string of characters to a bitmap. I have encountered many problems in achieving this. I understand the basic concept. I know I will need the alphanumeric characters "0123456789" and "XY"and"-"(for the -x and -y scale). I have seen many different examples and have tried extensively to follow JVitella's example here here

I am beginning to grasp the concept but as far as the my string goes I know I have 13 characters so how large should my bitmap be?

Also in Jvitelas example I am dumbfounded by the code:

Drawable background = context.getResources().getDrawable(R.drawable.background);

I dont understand what exactly is going on and when I code this I recieve a syntax error on context.

For my application I understand I would need to save the string into a bit map much like this. I would create a bitmap but how big should it be? Then I create a canvas from the bitmap and canvas.drawText into the bitmap.

  [ 0 1 2 3 4 ]
  | 5 6 7 8 9 |
  [ X Y Z     ]

Basically I am asking:

  • How to achieve the following bit map above?
  • How would I draw single digit numbers from the bit map?
  • How would I draw numbers with more than one digit?

解决方案

You're asking a lot of questions, but I'll try to answer a few:

  • so how large should my bitmap be?

    It's really up to you, depending on how crisp you want the text to be. You could allocate a huge bitmap with hundreds of pixels for each character that would zoom very well, or a very small bitmap with limited resolution. I'd say whatever "font size" you want to have, allocate at least that many pixels in height for each character. So if you want to draw something with a font size of "20", then maybe you need a bitmap 5x20 by 3x20 or 100x60.

  • How would I draw single digit numbers from the bit map?

    You'll draw a quad with opengl in the place where you want to draw a letter, and you use the texture coordinates of that quad to pick a letter.

    For example if I want to draw an X, then you draw a quad on the screen, and assign it's texcoords from (0,0) to (0.2, 0.33), which selects the left 1/5th of the texture, and the bottom 1/3rd of the texture. You'll see how a box like this lines up with the position of the "X" in your texture.

  • How would I draw numbers with more than one digit?

    You just draw two independent single digits right next to each other.


If your only goal here is to draw text in Android, it might be easier to just use a FrameLayout, and layer TextViews overtop of your GLSurfaceView. OpenGL isn't designed for text which makes it somewhat cumbersome.

这篇关于如何使文本位图和绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆