&&&&&&&&&&%%%%%%%%%%%#$##########
@@@@@@@@@@@@
两个小时精通 Android 开发之界面篇
笔者最近需要移植一个 J2ME 的项目到 Android 平台上,所以抽空研究了一下 Android 平台的
接口规范。
Android 平台除了在一些接口上采用了和 J2ME 不一样的名称之外,基本的接口内容还是一样
的,那么移植 J2ME 到 Android 平台最方便的方法就是利用 Android 平台的接口重写 J2ME 的接口,
本着这个思路笔者写了一个 Wrap 包,利用这个 Wrap 包,基本上 J2ME 程序不用修改,只需要重新
导入一下 重写后的 Graphics、Image 等类,就可以无缝的运行在 Android 平台上,所以笔者题目中
所说的“两个小时精通 Android”的前提是先精通 J2ME。
既然要重写 J2ME 下的这些接口,那么就要先了解 Android 和 J2ME 平台到底有哪些不同,然
后重写这部 分不同的接口即可。
Font 类
Android 平台里不再提供 J2ME 下面的 Font 类,而是以 Typeface 代替,这为读者提供了更多的
选择空间,因为 Typeface 是可以自定义的,但是为了无缝移植 J2ME 程序到 Android 平台上,笔者
封装了以个类似于 J2ME 平台下的 Font 类,如下:
import android.graphics.Typeface;
public class Font {
public static final int STYLE_PLAIN = 0;
public static final int STYLE_BOLD = 1;
public static final int STYLE_ITALIC = 2;
public static final int STYLE_UNDERLINED = 4;
public static final int SIZE_SMALL = 22;
public static final int SIZE_MEDIUM = 31;
public static final int SIZE_LARGE = 40;
public static final int FACE_SYSTEM = 0;
private int face;
private int style;
private int size;
private int baseline;
private int width;
private int height;
public void setWidth(int width)
{
this.width = size;
}
public void setHeight(int height)
{
this.height = height;
}
public int getWidth()
{
return size;
}
public int getHeight()
{
return height;
}
private Typeface iTypeface = Typeface.DEFAULT;
public Typeface getTypeface()
{
return iTypeface;
}
public void setSize(int fontSize)
{
size = fontSize;
}
public int getSize()
{
return size;
}
private static final Font DEFAULT_FONT = new Font(null,22);
public Font(Typeface aTypeface,int aSize)
{
if(iTypeface != null)
iTypeface = aTypeface;
size = aSize;
}
public static Font getFont(int fontSepcifier)
{
switch(fontSepcifier)
{
case SIZE_SMALL:
return new Font(null,22);
case SIZE_MEDIUM:
return new Font(null,31);
case SIZE_LARGE:
return new Font(null,40);
}
return DEFAULT_FONT;
}
public static Font getDefaultFont()
{
return DEFAULT_FONT;
}
public static Font getFont(int face,int style,int size)
{
switch(style)
{
case STYLE_PLAIN:
return new Font(Typeface.defaultFromStyle(Typeface.NORMAL),size);
case STYLE_BOLD:
return new Font(Typeface.defaultFromStyle(Typeface.BOLD),size);
case STYLE_ITALIC:
return new Font(Typeface.defaultFromStyle(Typeface.ITALIC),size);
}
return DEFAULT_FONT;
}
public int charWidth(char arg)
{
return size;
}
public int stringWidth(String arg)
{
return size*(arg.length());
}
}
Graphics 类
Android 平台下不再提供 Graphics 类进行“视图上下文”的控制,而是以 Canvas、Paint 联合进行控
制,笔者封装的 Graphics 类如下:
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
public class Graphics {
public static final int HCENTER = 1;
public static final int VCENTER = 2;
public static final int LEFT = 4;
public static final int RIGHT = 8;
public static final int TOP = 16;
public static final int BOTTOM = 32;
public static final int BASELINE = 64;
public static final int SOLID = 0;
public static final int DOTTED = 1;
private Bitmap bitmap;
private Canvas canvas;
private Paint paint;
private Rect clip;
private Font font;
public Graphics(Bitmap bitmap) {
this.bitmap = bitmap;
this.canvas = new Canvas(bitmap);
this.canvas.clipRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
this.canvas.save(Canvas.CLIP_SAVE_FLAG);
this.paint = new Paint();
this.clip = canvas.getClipBounds();
}
public void setAlign(int align)
{
if(LEFT == align
||(Graphics.LEFT | Graphics.TOP) == align
||(Graphics.LEFT | Graphics.BOTTOM) == align)
{
paint.setTextAlign(Align.LEFT);
}else if(HCENTER == align
||(Graphics.HCENTER|Graphics.TOP) == align)
{
paint.setTextAlign(Align.CENTER);
}else if(RIGHT == align
||(Graphics.RIGHT | Graphics.TOP) == align)
{
paint.setTextAlign(Align.RIGHT);
}
}
public void fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
{
paint.setStyle(Style.FILL);
canvas.drawRoundRect(new RectF(x,y,x+width,y+height), arcWidth, arcHeight, paint);
}
public void drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)
{
paint.setStyle(Style.STROKE);
canvas.drawRoundRect(new RectF(x,y,x+width,y+height), arcWidth, arcHeight, paint);
}
public void fillTriangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
paint.setStyle(Style.FILL);
canvas.drawLine(x1, y1, x2, y2, paint);
canvas.drawLine(x2, y2, x3, y3, paint);
canvas.drawLine(x3, y3, x1, y1, paint);
}
public void setFont(Font font)
{
paint.setTypeface(font.getTypeface());
paint.setTextSize(font.getSize());
FontMetrics fontMetrics = paint.getFontMetrics();
float height = fontMetrics.bottom-fontMetrics.top;
font.setHeight((int)height);
}
public void fillArc(int x,int y,int width,int height,int startAngle,int arcAngle)
{
paint.setStyle(Style.FILL);
canvas.drawArc(new RectF(x,y,width,height), startAngle, arcAngle, true, paint);
}
public void drawArc(int x,int y,int width,int height,int startAngle,int arcAngle)
{
paint.setStyle(Style.STROKE);
canvas.drawArc(new RectF(x,y,width,height), startAngle, arcAngle, true, paint);
}
public Font getFont()
{
return font;
}
public void setAntiAlias(boolean flag) {
paint.setAntiAlias(flag);
}