您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > 两个小时精通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类,如下:importandroid.graphics.Typeface;publicclassFont{publicstaticfinalintSTYLE_PLAIN=0;publicstaticfinalintSTYLE_BOLD=1;publicstaticfinalintSTYLE_ITALIC=2;publicstaticfinalintSTYLE_UNDERLINED=4;publicstaticfinalintSIZE_SMALL=22;publicstaticfinalintSIZE_MEDIUM=31;publicstaticfinalintSIZE_LARGE=40;publicstaticfinalintFACE_SYSTEM=0;privateintface;privateintstyle;privateintsize;privateintbaseline;privateintwidth;privateintheight;publicvoidsetWidth(intwidth){this.width=size;}publicvoidsetHeight(intheight){this.height=height;}publicintgetWidth(){returnsize;}publicintgetHeight(){returnheight;}privateTypefaceiTypeface=Typeface.DEFAULT;publicTypefacegetTypeface(){returniTypeface;}publicvoidsetSize(intfontSize){size=fontSize;}publicintgetSize(){returnsize;}privatestaticfinalFontDEFAULT_FONT=newFont(null,22);publicFont(TypefaceaTypeface,intaSize){if(iTypeface!=null)iTypeface=aTypeface;size=aSize;}publicstaticFontgetFont(intfontSepcifier){switch(fontSepcifier){caseSIZE_SMALL:returnnewFont(null,22);caseSIZE_MEDIUM:returnnewFont(null,31);caseSIZE_LARGE:returnnewFont(null,40);}returnDEFAULT_FONT;}publicstaticFontgetDefaultFont(){returnDEFAULT_FONT;}publicstaticFontgetFont(intface,intstyle,intsize){switch(style){caseSTYLE_PLAIN:returnnewFont(Typeface.defaultFromStyle(Typeface.NORMAL),size);caseSTYLE_BOLD:returnnewFont(Typeface.defaultFromStyle(Typeface.BOLD),size);caseSTYLE_ITALIC:returnnewFont(Typeface.defaultFromStyle(Typeface.ITALIC),size);}returnDEFAULT_FONT;}publicintcharWidth(chararg){returnsize;}publicintstringWidth(Stringarg){returnsize*(arg.length());}}ØGraphics类Android平台下不再提供Graphics类进行“视图上下文”的控制,而是以Canvas、Paint联合进行控制,笔者封装的Graphics类如下:importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Rect;importandroid.graphics.RectF;importandroid.graphics.Paint.Align;importandroid.graphics.Paint.FontMetrics;importandroid.graphics.Paint.Style;publicclassGraphics{publicstaticfinalintHCENTER=1;publicstaticfinalintVCENTER=2;publicstaticfinalintLEFT=4;publicstaticfinalintRIGHT=8;publicstaticfinalintTOP=16;publicstaticfinalintBOTTOM=32;publicstaticfinalintBASELINE=64;publicstaticfinalintSOLID=0;publicstaticfinalintDOTTED=1;privateBitmapbitmap;privateCanvascanvas;privatePaintpaint;privateRectclip;privateFontfont;publicGraphics(Bitmapbitmap){this.bitmap=bitmap;this.canvas=newCanvas(bitmap);this.canvas.clipRect(0,0,bitmap.getWidth(),bitmap.getHeight());this.canvas.save(Canvas.CLIP_SAVE_FLAG);this.paint=newPaint();this.clip=canvas.getClipBounds();}publicvoidsetAlign(intalign){if(LEFT==align||(Graphics.LEFT|Graphics.TOP)==align||(Graphics.LEFT|Graphics.BOTTOM)==align){paint.setTextAlign(Align.LEFT);}elseif(HCENTER==align||(Graphics.HCENTER|Graphics.TOP)==align){paint.setTextAlign(Align.CENTER);}elseif(RIGHT==align||(Graphics.RIGHT|Graphics.TOP)==align){paint.setTextAlign(Align.RIGHT);}}publicvoidfillRoundRect(intx,inty,intwidth,intheight,intarcWidth,intarcHeight){paint.setStyle(Style.FILL);canvas.drawRoundRect(newRectF(x,y,x+width,y+height),arcWidth,arcHeight,paint);}publicvoiddrawRoundRect(intx,inty,intwidth,intheight,intarcWidth,intarcHeight){paint.setStyle(Style.STROKE);canvas.drawRoundRect(newRectF(x,y,x+width,y+height),arcWidth,arcHeight,paint);}publicvoidfillTriangle(intx1,inty1,intx2,inty2,intx3,inty3){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);}publicvoidsetFont(Fontfont){paint.setTypeface(font.getTypeface());paint.setTextSize(font.getSize());FontMetricsfontMetrics=paint.getFontMetrics();floatheight=fontMetrics.bottom-fontMetrics.top;font.setHeight((int)height);}publicvoidfillArc(intx,inty,intwidth,intheight,intstartAngle,intarcAngle){paint.setStyle(Style.FILL);canvas.drawArc(newRectF(x,y,width,height),startAngle,arcAngle,true,paint);}publicvoiddrawArc(intx,inty,intwidth,intheight,intstartAngle,intarcAngle){paint.setStyle(Style.STROKE);canvas.drawArc(newRectF(x,y,width,height),startAngle,arcAngle,true,paint);}publicFontgetFont(){returnfont;}publicvoidsetAntiAlias(booleanflag){paint.setAntiAlias(flag);}publicvoidsetAlphaValue(intalpha){paint.setAlpha(alpha);}publicvoidsetAlpha(floatalpha){setAlphaValue((int)(255*alpha));}publicvoidtranslate(intx,inty){canvas.translate(x,y);}publicvoidsetColor(intrgb){paint.setColor(rgb);}publicvoidsetColor(intr,intg,intb){intargb=(0xff000000)+(r16)+(g8)+b;paint.setColor(argb);}publicvoiddrawImage(Imageimg,intx,inty){canvas.drawBitmap(img.getBitmap(),x,y,paint);
本文标题:两个小时精通Android开发之界面篇
链接地址:https://www.777doc.com/doc-4193854 .html