兩個小時精通Android開發之介面篇

來源:互聯網
上載者:User

作者:孫東風 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);

                   }

                  

                 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.