当前位置:操作系统 > 安卓/Android >>

两个小时精通Android开发之界面篇

作者:孙东风 2010-01-07(转载务必注明出处)

 

 


笔者最近需要移植一个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);

                   }

                  

                 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,