Android Opengl es 写字 笔记

转载自:http://blog.csdn.net/li6185377/article/details/7255809

由于Opengl 本身是没有DrawString 这种函数的 所以我们要实现最简单的办法         字写在图片上  在把图片转为Texture 进行绘图

下面是我封装好的类

[java]  view plain copy
  1. package ljh.opengl.command;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.Vector;  
  6. import ljh.opengl.GLEx;  
  7. import ljh.opengl.LColor;  
  8. import ljh.opengl.LTexture;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.Canvas;  
  11. import android.graphics.Bitmap.Config;  
  12. import android.graphics.Paint;  
  13.   
  14. public class GLFont {  
  15.           
  16.          //要打印的字  字体大小  字的信息   最大宽度  
  17.     public static Bitmap getImage(String str, int fontsize, Paint paint,  
  18.             int maxWidth) {  
  19.           
  20.         String[] text = StringFormat(str, maxWidth, fontsize);  
  21.         int[] count = getLinesMaxLength(text);  
  22.         Bitmap bitmap = Bitmap.createBitmap(count[0] * (fontsize / 2)  
  23.                 + count[1] * fontsize +5, (text.length) * fontsize,  
  24.                 Config.ARGB_8888);  
  25.         Canvas canvas = new Canvas(bitmap);  
  26.         paint.setTextSize(fontsize);  
  27.         for (int i = 0; i < text.length; i++) {  
  28.             canvas.drawText(text[i], 0, (i+1) * fontsize -3, paint);  
  29.         }  
  30.         return bitmap;  
  31.     }  
  32.         //具体的方法  还好我们前面实现了DrawTexture 我们这边就不用多写一次了  
  33.     public static void drawString(GLEx gl, String str, float x, float y,Paint paint,int maxWidth) {  
  34.           
  35.         if(str==null||str.trim()=="")  
  36.             return;  
  37.         String key = str.trim().toLowerCase();  
  38.         LTexture texture = data.get(key);  
  39.         if(texture==null)  
  40.         {  
  41.             Bitmap bitmap = getImage(str, (int)paint.getTextSize(), paint, maxWidth);  
  42.             texture = new LTexture(bitmap);  
  43.             data.put(key, texture);  
  44.         }  
  45.         gl.drawTexture(texture, x, y);  
  46.     }  
  47.         //保存着写过的字的纹理 当渲染到一定次数 还没使用的将被销毁  
  48.     public static HashMap<String, LTexture> data = new HashMap<String, LTexture>();  
  49.         //保存要销毁的纹理  
  50.     private static ArrayList<String> UnusedList = new ArrayList<String>();  
  51.       
  52.         //渲染线程没运行一次 次函数运行一次  
  53.         public static void update()  
  54.     {         
  55.            
  56.          for(String key:data.keySet())  
  57.          {  
  58.              LTexture texture = data.get(key);  
  59.              if(texture.isUnused())  
  60.              {  
  61.                  UnusedList.add(key);                  
  62.              }  
  63.              else  
  64.              {  
  65.                  texture.addUnused();  
  66.              }  
  67.          }  
  68.          for(String key:UnusedList)  
  69.          {  
  70.              GLEx.glex.delete(data.remove(key));  
  71.          }  
  72.          UnusedList.clear();           
  73.     }  
  74.     /** 
  75.      * 返回字数最多的那个行中中英文的数量 
  76.      *  
  77.      * @param lines 
  78.      * @return int[0] 英文的数量 int[1] 中文的数量 
  79.      */  
  80.     public static int[] getLinesMaxLength(String[] lines) {  
  81.         int max = 0, index = 0;  
  82.         for (int i = 0; i < lines.length; i++) {  
  83.             if (max < lines[i].getBytes().length) {  
  84.                 max = lines[i].getBytes().length;  
  85.                 index = i;  
  86.             }  
  87.         }  
  88.         int[] count = new int[2];  
  89.         for (int i = 0; i < lines[index].length(); i++) {  
  90.             if (lines[index].charAt(i) > 255) {  
  91.                 count[1]++;  
  92.             } else {  
  93.                 count[0]++;  
  94.             }  
  95.         }  
  96.         return count;  
  97.     }  
  98.         //对String 进行分段  
  99.     public static String[] StringFormat(String text, int maxWidth, int fontSize) {  
  100.   
  101.         String[] result = null;  
  102.         Vector<String> tempR = new Vector<String>();  
  103.         int lines = 0;  
  104.         int len = text.length();  
  105.         int index0 = 0;  
  106.         int index1 = 0;  
  107.         boolean wrap;  
  108.         while (true) {  
  109.             int widthes = 0;  
  110.             wrap = false;  
  111.             for (index0 = index1; index1 < len; index1++) {  
  112.                 if (text.charAt(index1) == '\n') {  
  113.                     index1++;  
  114.                     wrap = true;  
  115.                     break;  
  116.                 }  
  117.                 widthes = fontSize + widthes;  
  118.                 if (widthes > maxWidth) {  
  119.                     break;  
  120.                 }  
  121.             }  
  122.             lines++;  
  123.             if (wrap) {  
  124.                 tempR.addElement(text.substring(index0, index1 - 1));  
  125.             } else {  
  126.                 tempR.addElement(text.substring(index0, index1));  
  127.             }  
  128.             if (index1 >= len) {  
  129.                 break;  
  130.             }  
  131.         }  
  132.         result = new String[lines];  
  133.         tempR.copyInto(result);  
  134.         return result;  
  135.     }  
  136.   
  137. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值