为了让J2ME游戏无需大的改动就可以在Android的平台下,我是在利用Android平台的类库拼接成J2ME类,如用Android的canvas类和paint类拼接成J2me的Graphics类,用Bitmap重写J2ME的Image类。最终得到一个中间件包!
Font类的代码:
import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.Typeface; //复写J2ME的Font类中的变量和函数 public class Font { public static final int FACE_MONOSPACE = 32; public static final int FACE_PROPORTIONAL = 64; public static final int FACE_SYSTEM = 0; public static final int FONT_INPUT_TEXT = 1; public static final int FONT_STATIC_TEXT = 0; public static final int SIZE_LARGE = 32; public static final int SIZE_MEDIUM = 24; public static final int SIZE_SMALL = 16; public static final int STYLE_BOLD = 1; public static final int STYLE_ITALIC = 2; public static final int STYLE_PLAIN = 0; public static final int STYLE_UNDERLINED = 4; //在J2ME的graphics 在android 就是Paint //Paint 其实要比graphics 分装的东西要多,包括字体的大小也是有其控制的 private Paint paint; private int face, style, size; //measureText 来测量字体的长度 int charsWidth(char[] ch, int offset, int length) { return (int) paint.measureText(ch, offset, length); } int charWidth(char ch) { char[] charArr = new char[1]; charArr[0] = ch; return (int) paint.measureText(charArr, 0, charArr.length); } int getBaselinePosition() { FontMetrics fm = paint.getFontMetrics(); return (int) fm.bottom; } private Font() { face = FACE_SYSTEM; style = STYLE_PLAIN; size = SIZE_MEDIUM; setPaintAtt(paint); } private Font(int face, int style, int size) { this.face = face; this.style = style; this.size = size; setPaintAtt(paint); } private Font(Paint paint, int face, int style, int size) { setPaintAtt(paint); } //android 中字体的样式都是由Typeface控制 public void setPaintAtt(Paint paint) { this.paint = paint; if (paint == null) paint = new Paint(); switch (face) { case FACE_MONOSPACE: paint.setTypeface(Typeface.MONOSPACE); break; case FACE_PROPORTIONAL: break; case FACE_SYSTEM: paint.setTypeface(Typeface.DEFAULT); break; } paint.setAntiAlias(true); switch (style) { case STYLE_BOLD: paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); break; case STYLE_ITALIC: paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC)); break; case STYLE_PLAIN: paint.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); break; case STYLE_UNDERLINED: // paint.setTypeface(Typeface.SERIF); break; } paint.setTextSize(size); } public static Font getFont(int face, int style, int size) { Font font = new Font(face, style, size); return font; } public static Font getFont(int fontSpecifier) { switch (fontSpecifier) { case FONT_INPUT_TEXT: return getFont(FACE_SYSTEM, SIZE_MEDIUM, STYLE_UNDERLINED); case FONT_STATIC_TEXT: return getFont(FACE_SYSTEM, SIZE_MEDIUM, STYLE_ITALIC); default: return new Font(); } } public static Font getDefaultFont() { return new Font(); } public static Font getFont(Paint paint) { Font font = new Font(); font.paint = paint; font.size = (int) paint.getTextSize(); return font; } public int getFace() { // Typeface tf = paint.getTypeface(); return 0; } //这里字体的高度复写了一下,大家请参考一下api public int getHeight() { FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.ascent); } public int getSize() { return (int) paint.getTextSize(); } public int getStyle() { Typeface tf = paint.getTypeface(); return tf.getStyle(); } public boolean isBold() { Typeface tf = paint.getTypeface(); return tf.isBold(); } public boolean isItalic() { Typeface tf = paint.getTypeface(); return tf.isItalic(); } public boolean isPlain() { Typeface tf = paint.getTypeface(); return tf.isItalic(); } public boolean isUnderlined() { return paint.isUnderlineText(); } public int stringWidth(String str) { return (int) paint.measureText(str); } public int substringWidth(String str, int offset, int len) { return (int) paint.measureText(str.substring(offset, offset + len)); } }
Image类
import java.io.IOException; import java.io.InputStream; import lxs.slg.Game; import android.content.res.AssetManager; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; public class Image { //在android 中 图片对象是Bitmap private Bitmap img; private int alpha; private Image() { img = null; alpha = 0xff; } private Image(Bitmap img, int alpha) { this.img = img; this.alpha = alpha; } private Image(Image source) { this.img = source.img; this.alpha = source.alpha; } public static Image createImage(String src) { if (src.startsWith("/")) { src = src.substring(1); } //当前的activity对象 //如下是标准的读取Bitmap方法 Resources resources = Game.father.getResources(); AssetManager assetManager = resources.getAssets(); InputStream in = null; Bitmap img = null; try { in = assetManager.open(src); img = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e) { e.printStackTrace(); } return new Image(img, 0xff); } public static Image createImage(byte[] imageData, int imageOffset, int imageLength) { //从byte对象创建一个Bitmap 是由BitmapFactory函数负责的 Bitmap img = BitmapFactory.decodeByteArray(imageData, imageOffset, imageLength); return new Image(img, 0xff); } public static Image createImage(Image source) { return new Image(source); } public static Image createImage(Image image, int x, int y, int width, int height, int transform) { Bitmap bmp = Bitmap.createBitmap(image.img, x, y, width, height); Paint paint = new Paint(); paint.setAlpha(image.alpha); Image tImage = createImage(width, height); Canvas canvas = new Canvas(tImage.img); //调用Graphics函数 获取旋转矩阵 Matrix mMatrix = Graphics.getTransformMetrix(transform, width, height); //调用如下的函数将其绘制在兴建的Bitmap对象上 canvas.drawBitmap(bmp, mMatrix, paint); return tImage; } public static Image createImage(InputStream stream) { Bitmap img = BitmapFactory.decodeStream(stream); return new Image(img, 0xff); } public static Image createImage(int width, int height) { //android 中创建Image 对象的函数 Bitmap img = Bitmap .createBitmap(width, height, Bitmap.Config.ARGB_8888); return new Image(img, 0xff); } public static Image createRGBImage(int[] rgb, int width, int height, boolean processAlpha) { Bitmap img; if (processAlpha) { img = Bitmap.createBitmap(rgb, width, height, Bitmap.Config.ARGB_8888); } else { //不需要支持透明的图片,我们采用差分颜色图片点阵标识 img = Bitmap .createBitmap(rgb, width, height, Bitmap.Config.RGB_565); } return new Image(img, 0xff); } //获取图片的绘制对象 public Graphics getGraphics() { Canvas canvas = new Canvas(img); Paint paint = new Paint(); paint.setAlpha(alpha); return new Graphics(canvas, paint); } public void setAlpha(int alpha) { this.alpha = alpha; } public int getAlpha() { return alpha; } public Bitmap getImg() { return img; } public int getHeight() { return img.getHeight(); } public void getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height) { img.getPixels(rgbData, offset, scanlength, x, y, width, height); } public int getWidth() { return img.getWidth(); } boolean isMutable() { return img.isMutable(); } public void replaceColor(int newColor, int oldColor) { int width = img.getWidth(); int height = img.getHeight(); int[] pixels = new int[width * height]; img.getPixels(pixels, 0, width, 0, 0, width, height); for (int i = 0; i < pixels.length; i++) { if ((pixels[i] | 0x00ffffff) == (oldColor | 0x00ffffff)) { pixels[i] = oldColor; } } img = Bitmap.createBitmap(pixels, 0, 0, height, height, Bitmap.Config.ARGB_8888); } }