android 点阵字库

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:

public class Font16 {
	private Context context;

	public Font16(Context context) {
		this.context = context;
	}

	private final static String ENCODE = "GB2312";
	private final static String ZK16 = "HZK16";

	private boolean[][] arr;
	int all_16_32 = 16;
	int all_2_4 = 2;
	int all_32_128 = 32;

	public boolean[][] drawString(String str) {
		byte[] data = null;
		int[] code = null;
		int byteCount;
		int lCount;

		arr = new boolean[all_16_32][all_16_32];
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) < 0x80) {
				continue;
			}
			code = getByteCode(str.substring(i, i + 1));
			data = read(code[0], code[1]);
			byteCount = 0;
			for (int line = 0; line < all_16_32; line++) {
				lCount = 0;
				for (int k = 0; k < all_2_4; k++) {
					for (int j = 0; j < 8; j++) {
						if (((data[byteCount] >> (7 - j)) & 0x1) == 1) {
							arr[line][lCount] = true;
							System.out.print("@");
						} else {
							System.out.print(" ");
							arr[line][lCount] = false;
						}
						lCount++;
					}
					byteCount++;
				}
				System.out.println();
			}
		}
		return arr;
	}

	protected byte[] read(int areaCode, int posCode) {
		byte[] data = null;
		try {
			int area = areaCode - 0xa0;
			int pos = posCode - 0xa0;

			InputStream in = context.getResources().getAssets().open(ZK16);
			long offset = all_32_128 * ((area - 1) * 94 + pos - 1);
			in.skip(offset);
			data = new byte[all_32_128];
			in.read(data, 0, all_32_128);
			in.close();
		} catch (Exception ex) {
		}
		return data;
	}

	protected int[] getByteCode(String str) {
		int[] byteCode = new int[2];
		try {
			byte[] data = str.getBytes(ENCODE);
			byteCode[0] = data[0] < 0 ? 256 + data[0] : data[0];
			byteCode[1] = data[1] < 0 ? 256 + data[1] : data[1];
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return byteCode;
	}

}

 自定义SurfaceView:

public class MySurfaceView extends SurfaceView {
	private Context mContext;
	private SurfaceHolder holder;

	public MySurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		holder = this.getHolder();
	}

	public MySurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		holder = this.getHolder();
	}

	public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		holder = this.getHolder();
	}

	/**
	 * show font
	 * 
	 * @param font_kind
	 *            the font type, has 16,24,32
	 * @param font
	 *            draw font
	 * @param startx
	 *            the font coordinate start x value
	 * @param starty
	 *            the font coordinate start y value
	 * @param beishu
	 *            the font magnification multiple
	 * @param type
	 *            draw font use icon,1 is flower,2 is love
	 */

	public void show_font16(int font_kind, String font, int startx, int starty, int beishu, int type) {
		boolean[][] arr = null;
		int weith = 16;
		int height = 16;
		if (font_kind == 16) {
			weith = 16;
			height = 16;
			arr = new boolean[weith][height];
			Font16 font16 = new Font16(mContext);
			arr = font16.drawString(font);
		} else if (font_kind == 24) {
			weith = 24;
			height = 24;
			arr = new boolean[weith][height];
			Font24 font24 = new Font24(mContext);
			arr = font24.drawString(font);
		} else {
			weith = 32;
			height = 32;
			arr = new boolean[weith][height];
			Font32 font32 = new Font32(mContext);
			arr = font32.drawString(font);
		}

		for (int i = 0; i < weith; i++) {
			for (int j = 0; j < height; j++) {
				try {
					Thread.sleep(25);
				} catch (InterruptedException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				float x = (float) j;
				float y = (float) i;
				if (arr[i][j]) {
					Bitmap bitmap = null;
					if (type == 1) {
						bitmap = BitmapFactory.decodeStream(mContext.getResources().openRawResource(R.drawable.hua));
					} else if (type == 2) {
						bitmap = BitmapFactory.decodeStream(mContext.getResources().openRawResource(R.drawable.love));
					}
					int bw = bitmap.getWidth();
					int bh = bitmap.getHeight();
					synchronized (holder) {
						Canvas c = null;
						try {
							c = holder.lockCanvas(new Rect(startx + (int) x * beishu, starty + (int) y * beishu, startx + (int) x * beishu
									+ bw, starty + (int) y * beishu + bh));

							Paint p = new Paint();
							p.setColor(Color.RED);
							c.drawBitmap(bitmap, startx + x * beishu, starty + y * beishu, p);

						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							try {
								if (c != null) {
									holder.unlockCanvasAndPost(c);// 结束锁定画图,并提交改变。
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
			}

		}

	}

}

 

 

你可能感兴趣的:(android)