Display text (for example, rating) that changes on each frame

Is there an easy way to display plain white text without any textures in the upper right corner that changes on every frame in the class that implements Renderer in the onDrawFrame method? This is 2D, so there is no need to do any transformations, I believe. I found many tutorials, but they all seem too complicated with too much code. Maybe this can be done with only a few lines of code? If this is not possible, then what is the easiest way to do this without modifying too much existing code? Please show the full code as I am a beginner.

+7
source share
8 answers

Renderer is an OpenGL method. OpenGL ES does not essentially support text. This leaves the following possibilities:

  • Use a texture, draw it using the Android Canvas class.
  • Use the library. This will most likely also lead to texture.
  • Create text using polygons.

Since your question requires a solution that does not draw a texture, then 1 and 2 both come out. And since you need something simple, 3 too. Leaving no good solutions.

An alternative to considering is to use a different view on top of the GLSurfaceView used. You can use RelativeLayout to easily add two views, one of which is a text view that you can snap in the upper right corner of the screen. Pseudocode below:

<RelativeLayout ... /> <GLSurfaceView ... /> <TextView ... /> </RelativeLayout> 

This approach has the advantage of pulling you from OpenGL requirements to make your text. I have done this successfully in several of my applications.

+11
source

You can take a look at the Demos API project, which comes with sample Android SDKs. There is an OpenGL Sprite Text sample that does exactly what you want.

You can see the demo by installing the Demos API on an emulator (or device) and going to: Demos API> Graphics> OpenGL ES> Sprite Text

Here is a screenshot:

enter image description here

The text in the lower right corner of the screen changes on each frame in the onDrawFrame method of the renderer.

You can find the source code in the Android SDK sample directory: ApiDemos \ SRC \ COM \ e.g. \ Android \ APIs \ graphics \ spritetext

You will need to copy a few files into your project, and with very few lines of code you can achieve the same functionality.

Sample application : I created a sample application to demonstrate it by reusing code from the Demos API project. You can get it from here - OpenGLText_eclipse_project.zip

In short, you can fully use Grid.java, LabelMaker.java, NumericSprite.java. Then add a few lines of code to your Renderer to use the above classes as shown in my TextRenderer.java

Here's what it looks like with an updated score.

enter image description here

Basically, rendering an estimate is as simple as this:

 LabelMaker mLabels = new LabelMaker(); mLabels.initialize(gl); mLabels.beginAdding(gl); int mLabelMsPF = mLabels.add(gl, "Score:", mLabelPaint); mLabels.endAdding(gl); mLabels.beginDrawing(gl, mWidth, mHeight); mLabels.draw(gl, 0, 0, mLabelMsPF); mLabels.endDrawing(gl); NumericSprite mNumericSprite = new NumericSprite(); mNumericSprite.setValue(16); mNumericSprite.draw(gl, 0, 0, mWidth, mHeight); 
+7
source

Try looking at the CodeHead Bitmap and Android Loader font generator and try using this code.

CBFG

Android downloader

+3
source

I used a drawText bitmap, but I still had to use it as a texture, possibly with a smaller bitmap and font size

 bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888); canvas = new Canvas(bitmap); h = (int)((((System.currentTimeMillis()/1000)/60)/60)%60); m = (int)(((System.currentTimeMillis()/1000)/60)%60); s = (int)((System.currentTimeMillis()/1000)%60); paint = new Paint(); paint.setColor(Color.MAGENTA); paint.setTextAlign(Align.CENTER); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setTextSize(55); time = String.valueOf(h) + "h" + String.valueOf(m) + "m" + String.valueOf(s) + "s"; canvas.drawText(time, bitmap.getWidth()/2, 100, paint); int[] textureIds = new int[1]; gl.glGenTextures(1, textureIds, 0); textureId = textureIds[0]; gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); gl.glBindTexture(GL10.GL_TEXTURE_2D, 0); 

if you go here in chapter 9 src / com.badlogic.androidgames.framework.gl.Font.java is what I use fonts for the bitmap and I generate bitmaps using the Bitheads Codeheads font generator

+2
source

The text should be in a different layer than OpenGL.

 <FrameLayout> <GLSurfaceView/> <LinearLayout android:layout_gravity="top|right" > <TextView android:id="@+id/score" ... /> </LinearLayout> </FrameLayout> 

FrameLayout allows views to stack one on top of the other. Then set the text with:

 ( (TextView) findViewById(R.id.score) ).setText( "Your Score" ); 

You may need to force a redraw of the evaluation layout after each rendering of the GLSurfaceView, to avoid openGL drawing on top of the text. (not sure about that)

For maximum performance, make the text area as large as possible (wrap_content for LinearLayout width and height)

+2
source

In my opinion, you can use RelativeLayout to transfer GLSurfaceView, as well as another TextView to place where you want. Then in the Renderer constructor, pass the TextView link GLES20TriangleRenderer (context context, TextView tv) In the onDrawFrame method, call

  ((Activity)mContext).runOnUiThread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub tv.setText(Math.random() + " random"); } }); 
+1
source

You need to draw your assessment text in a bitmap and after using this bitmap as a texture.

To draw text on a bitmap, you can use this code:

 // Create and draw a texto to your bitmap. Bitmap bm = Bitmap.createBitmap(300, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bm); Paint p = new Paint(); p.setColor(Color.RED); c.drawText("YOUR SCORE", 0, 0, new Paint()); 
+1
source

You can simply attach the TextView to the Activity WindowManager and show it from there. Properly.

 TextView tv = new TextView(this); tv.setText("FPS: 0"); //get WindowManager from Activity WindowManager w = this.getWindowManager(); //setup view LayoutParams WindowManager.LayoutParams lp = new WindowManager.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSPARENT); //position lp.gravity = Gravity.RIGHT | Gravity.TOP; //attach view w.addView(tv,lp); 

Then update this with

 activity.runOnUiThread(new Runnable(){ @Override public void run() { tv.setText("FPS: " + fps); } }); 

enter image description here

0
source

All Articles