How to Render text to bitmap and draw it

谁说胖子不能爱 提交于 2019-12-12 15:23:18

问题


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?

回答1:


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.



来源:https://stackoverflow.com/questions/13654964/how-to-render-text-to-bitmap-and-draw-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!