为了更好的查看程序运行速度,在界面的右下方增加每帧所用时间显示。
该文本是固定在屏幕右下角显示,原理为将要显示的文本绘在图片中,然后将图片直接贴到屏幕的右下角,具体编码如下:
if(mTextureID==-1){
int fontSize = 32;
//设置字体、字体大小和字体颜色
Paint p = new Paint();
String familyName = "Times New Roman";
Typeface font = Typeface.create(familyName, Typeface.NORMAL);
p.setColor(Color.RED);
p.setTypeface(font);
p.setTextSize(fontSize);
//在Bitmap上绘制文字
String text = "旋转方块";
int textWidth = (int) Math.ceil(p.measureText(text));
int textHeight = (int) Math.ceil(-p.ascent()) + (int) Math.ceil(p.descent());
imageWidth = textWidth;
imageHeight = textHeight + 10;
Bitmap bitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text,0,textHeight, p);
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
mTextureID = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
// Use Nearest for performance.
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.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,GL10.GL_CLAMP_TO_EDGE);
//GL10.GL_MODULATE
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,GL10.GL_REPLACE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
int[] crop = {0, imageHeight, imageWidth, -imageHeight};
((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D,GL11Ext.GL_TEXTURE_CROP_RECT_OES,crop, 0);
//将纹理直接画到屏幕中某位置
((GL11Ext)gl).glDrawTexiOES((AppConfig.gpViewport[2] - imageWidth)/2, 10 , 0,imageWidth, imageHeight);
gl.glDisable(GL10.GL_TEXTURE_2D);
全部源代码下载地址:
http://download.csdn.net/detail/tomatozq/4340801