项目文件: http://download.csdn.net/detail/li6185377/4194984
由于Opengl 本身是没有DrawString 这种函数的 所以我们要实现最简单的办法 字写在图片上 在把图片转为Texture 进行绘图
下面是我封装好的类
package ljh.opengl.command;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import ljh.opengl.GLEx;
import ljh.opengl.LColor;
import ljh.opengl.LTexture;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.Paint;
public class GLFont {
//要打印的字 字体大小 字的信息 最大宽度
public static Bitmap getImage(String str, int fontsize, Paint paint,
int maxWidth) {
String[] text = StringFormat(str, maxWidth, fontsize);
int[] count = getLinesMaxLength(text);
Bitmap bitmap = Bitmap.createBitmap(count[0] * (fontsize / 2)
+ count[1] * fontsize +5, (text.length) * fontsize,
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
paint.setTextSize(fontsize);
for (int i = 0; i < text.length; i++) {
canvas.drawText(text[i], 0, (i+1) * fontsize -3, paint);
}
return bitmap;
}
//具体的方法 还好我们前面实现了DrawTexture 我们这边就不用多写一次了
public static void drawString(GLEx gl, String str, float x, float y,Paint paint,int maxWidth) {
if(str==null||str.trim()=="")
return;
String key = str.trim().toLowerCase();
LTexture texture = data.get(key);
if(texture==null)
{
Bitmap bitmap = getImage(str, (int)paint.getTextSize(), paint, maxWidth);
texture = new LTexture(bitmap);
data.put(key, texture);
}
gl.drawTexture(texture, x, y);
}
//保存着写过的字的纹理 当渲染到一定次数 还没使用的将被销毁
public static HashMap<String, LTexture> data = new HashMap<String, LTexture>();
//保存要销毁的纹理
private static ArrayList<String> UnusedList = new ArrayList<String>();
//渲染线程没运行一次 次函数运行一次
public static void update()
{
for(String key:data.keySet())
{
LTexture texture = data.get(key);
if(texture.isUnused())
{
UnusedList.add(key);
}
else
{
texture.addUnused();
}
}
for(String key:UnusedList)
{
GLEx.glex.delete(data.remove(key));
}
UnusedList.clear();
}
/**
* 返回字数最多的那个行中中英文的数量
*
* @param lines
* @return int[0] 英文的数量 int[1] 中文的数量
*/
public static int[] getLinesMaxLength(String[] lines) {
int max = 0, index = 0;
for (int i = 0; i < lines.length; i++) {
if (max < lines[i].getBytes().length) {
max = lines[i].getBytes().length;
index = i;
}
}
int[] count = new int[2];
for (int i = 0; i < lines[index].length(); i++) {
if (lines[index].charAt(i) > 255) {
count[1]++;
} else {
count[0]++;
}
}
return count;
}
//对String 进行分段
public static String[] StringFormat(String text, int maxWidth, int fontSize) {
String[] result = null;
Vector<String> tempR = new Vector<String>();
int lines = 0;
int len = text.length();
int index0 = 0;
int index1 = 0;
boolean wrap;
while (true) {
int widthes = 0;
wrap = false;
for (index0 = index1; index1 < len; index1++) {
if (text.charAt(index1) == '\n') {
index1++;
wrap = true;
break;
}
widthes = fontSize + widthes;
if (widthes > maxWidth) {
break;
}
}
lines++;
if (wrap) {
tempR.addElement(text.substring(index0, index1 - 1));
} else {
tempR.addElement(text.substring(index0, index1));
}
if (index1 >= len) {
break;
}
}
result = new String[lines];
tempR.copyInto(result);
return result;
}
}
下一章 就是对GL10的封装了
本文介绍如何在Android中使用OpenGL ES实现文字绘制,由于OpenGL本身不支持DrawString,作者通过将文字写在图片上并转换为纹理进行绘图的方法来实现。提供了项目文件的下载链接。
&spm=1001.2101.3001.5002&articleId=7255809&d=1&t=3&u=24a7a1cd9d3447b982cc18db38da1928)
6934

被折叠的 条评论
为什么被折叠?



