android 点阵字库

来源:互联网 发布:但我知你的心 编辑:程序博客网 时间:2024/05/20 00:15

1.点阵字库

 

       点阵字库是把每一个汉字都分成16×16或24×24个点,然后用每个点的虚实来表示汉字的轮廓,常用来作为显示字库使用,这类点阵字库汉字最大的缺点是不能放大,一旦放大后就会发现文字边缘的锯齿。 

矢量字库保存的是对每一个汉字的描述信息,比如一个笔划的起始、终止坐标,半径、弧度等等。在显示、打印这一类字库时,要经过一系列的数学运算才能输出结果,但是这一类字库保存的汉字理论上可以被无限地放大,笔划轮廓仍然能保持圆滑,打印时使用的字库均为此类字库.

2.点阵字库结构

      在汉字的点阵字库中,每个字节的每个位都代表一个汉字的一个点,每个汉字都是由一个矩形的点阵组成,0代表没有,1代表有点,将0和1分别用不同颜色画出,就形成了一个汉字,常用的点阵矩阵有12*12, 14*14, 16*16三种字库。  字库根据字节所表示点的不同有分为横向矩阵和纵向矩阵,目前多数的字库都是横向矩阵的存储方式(用得最多的应该是早期UCDOS字库),纵向矩阵一般是因为有某些液晶是采用纵向扫描显示法,为了提高显示速度,于是便把字库矩阵做成纵向,省得在显示时还要做矩阵转换。我们接下去所描述的都是指横向矩阵字库。

       对于16*16的矩阵来说,它所需要的位数共是16*16=256个位,每个字节为8位,因此,每个汉字都需要用256/8=32个字节来表示。  即每两个字节代表一行的16个点,共需要16行,显示汉字时,只需一次性读取32个字节,并将每两个字节为一行打印出来,即可形成一个汉字.

16*16:

Java代码  收藏代码
  1. public class Font16 {  
  2.     private Context context;  
  3.   
  4.     public Font16(Context context) {  
  5.         this.context = context;  
  6.     }  
  7.   
  8.     private final static String ENCODE = "GB2312";  
  9.     private final static String ZK16 = "HZK16";  
  10.   
  11.     private boolean[][] arr;  
  12.     int all_16_32 = 16;  
  13.     int all_2_4 = 2;  
  14.     int all_32_128 = 32;  
  15.   
  16.     public boolean[][] drawString(String str) {  
  17.         byte[] data = null;  
  18.         int[] code = null;  
  19.         int byteCount;  
  20.         int lCount;  
  21.   
  22.         arr = new boolean[all_16_32][all_16_32];  
  23.         for (int i = 0; i < str.length(); i++) {  
  24.             if (str.charAt(i) < 0x80) {  
  25.                 continue;  
  26.             }  
  27.             code = getByteCode(str.substring(i, i + 1));  
  28.             data = read(code[0], code[1]);  
  29.             byteCount = 0;  
  30.             for (int line = 0; line < all_16_32; line++) {  
  31.                 lCount = 0;  
  32.                 for (int k = 0; k < all_2_4; k++) {  
  33.                     for (int j = 0; j < 8; j++) {  
  34.                         if (((data[byteCount] >> (7 - j)) & 0x1) == 1) {  
  35.                             arr[line][lCount] = true;  
  36.                             System.out.print("@");  
  37.                         } else {  
  38.                             System.out.print(" ");  
  39.                             arr[line][lCount] = false;  
  40.                         }  
  41.                         lCount++;  
  42.                     }  
  43.                     byteCount++;  
  44.                 }  
  45.                 System.out.println();  
  46.             }  
  47.         }  
  48.         return arr;  
  49.     }  
  50.   
  51.     protected byte[] read(int areaCode, int posCode) {  
  52.         byte[] data = null;  
  53.         try {  
  54.             int area = areaCode - 0xa0;  
  55.             int pos = posCode - 0xa0;  
  56.   
  57.             InputStream in = context.getResources().getAssets().open(ZK16);  
  58.             long offset = all_32_128 * ((area - 1) * 94 + pos - 1);  
  59.             in.skip(offset);  
  60.             data = new byte[all_32_128];  
  61.             in.read(data, 0, all_32_128);  
  62.             in.close();  
  63.         } catch (Exception ex) {  
  64.         }  
  65.         return data;  
  66.     }  
  67.   
  68.     protected int[] getByteCode(String str) {  
  69.         int[] byteCode = new int[2];  
  70.         try {  
  71.             byte[] data = str.getBytes(ENCODE);  
  72.             byteCode[0] = data[0] < 0 ? 256 + data[0] : data[0];  
  73.             byteCode[1] = data[1] < 0 ? 256 + data[1] : data[1];  
  74.         } catch (Exception ex) {  
  75.             ex.printStackTrace();  
  76.         }  
  77.         return byteCode;  
  78.     }  
  79.   
  80. }  

 自定义SurfaceView:

Java代码  收藏代码
  1. public class MySurfaceView extends SurfaceView {  
  2.     private Context mContext;  
  3.     private SurfaceHolder holder;  
  4.   
  5.     public MySurfaceView(Context context) {  
  6.         super(context);  
  7.         // TODO Auto-generated constructor stub  
  8.         this.mContext = context;  
  9.         holder = this.getHolder();  
  10.     }  
  11.   
  12.     public MySurfaceView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.         // TODO Auto-generated constructor stub  
  15.         this.mContext = context;  
  16.         holder = this.getHolder();  
  17.     }  
  18.   
  19.     public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {  
  20.         super(context, attrs, defStyleAttr);  
  21.         // TODO Auto-generated constructor stub  
  22.         this.mContext = context;  
  23.         holder = this.getHolder();  
  24.     }  
  25.   
  26.     /** 
  27.      * show font 
  28.      *  
  29.      * @param font_kind 
  30.      *            the font type, has 16,24,32 
  31.      * @param font 
  32.      *            draw font 
  33.      * @param startx 
  34.      *            the font coordinate start x value 
  35.      * @param starty 
  36.      *            the font coordinate start y value 
  37.      * @param beishu 
  38.      *            the font magnification multiple 
  39.      * @param type 
  40.      *            draw font use icon,1 is flower,2 is love 
  41.      */  
  42.   
  43.     public void show_font16(int font_kind, String font, int startx, int starty, int beishu, int type) {  
  44.         boolean[][] arr = null;  
  45.         int weith = 16;  
  46.         int height = 16;  
  47.         if (font_kind == 16) {  
  48.             weith = 16;  
  49.             height = 16;  
  50.             arr = new boolean[weith][height];  
  51.             Font16 font16 = new Font16(mContext);  
  52.             arr = font16.drawString(font);  
  53.         } else if (font_kind == 24) {  
  54.             weith = 24;  
  55.             height = 24;  
  56.             arr = new boolean[weith][height];  
  57.             Font24 font24 = new Font24(mContext);  
  58.             arr = font24.drawString(font);  
  59.         } else {  
  60.             weith = 32;  
  61.             height = 32;  
  62.             arr = new boolean[weith][height];  
  63.             Font32 font32 = new Font32(mContext);  
  64.             arr = font32.drawString(font);  
  65.         }  
  66.   
  67.         for (int i = 0; i < weith; i++) {  
  68.             for (int j = 0; j < height; j++) {  
  69.                 try {  
  70.                     Thread.sleep(25);  
  71.                 } catch (InterruptedException e1) {  
  72.                     // TODO 自动生成的 catch 块  
  73.                     e1.printStackTrace();  
  74.                 }  
  75.                 float x = (float) j;  
  76.                 float y = (float) i;  
  77.                 if (arr[i][j]) {  
  78.                     Bitmap bitmap = null;  
  79.                     if (type == 1) {  
  80.                         bitmap = BitmapFactory.decodeStream(mContext.getResources().openRawResource(R.drawable.hua));  
  81.                     } else if (type == 2) {  
  82.                         bitmap = BitmapFactory.decodeStream(mContext.getResources().openRawResource(R.drawable.love));  
  83.                     }  
  84.                     int bw = bitmap.getWidth();  
  85.                     int bh = bitmap.getHeight();  
  86.                     synchronized (holder) {  
  87.                         Canvas c = null;  
  88.                         try {  
  89.                             c = holder.lockCanvas(new Rect(startx + (int) x * beishu, starty + (int) y * beishu, startx + (int) x * beishu  
  90.                                     + bw, starty + (int) y * beishu + bh));  
  91.   
  92.                             Paint p = new Paint();  
  93.                             p.setColor(Color.RED);  
  94.                             c.drawBitmap(bitmap, startx + x * beishu, starty + y * beishu, p);  
  95.   
  96.                         } catch (Exception e) {  
  97.                             e.printStackTrace();  
  98.                         } finally {  
  99.                             try {  
  100.                                 if (c != null) {  
  101.                                     holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。  
  102.                                 }  
  103.                             } catch (Exception e) {  
  104.                                 e.printStackTrace();  
  105.                             }  
  106.                         }  
  107.                     }  
  108.                 }  
  109.             }  
  110.   
  111.         }  
  112.   
  113.     }  
  114.   
  115. }  

 

 

  • Attached.zip (1009.7 KB)
  • 下载次数: 0
0 0
原创粉丝点击