Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ApiDemos 全解析(第 199 篇):總整理與索引

編輯:關於 Android 程式設計

ApiDemos 是 Android SDK 附帶的範例合集,也是理解平台 API 最直接的入口。前面近兩百篇下來,我們把其中與 OpenGL ES 相關以外的範例幾乎都走過一遍,這一篇做個收束,把整體結構與索引整理出來,方便日後回頭查閱。

ApiDemos 的分類架構

官方把範例分成七大類,每類再往下切細項,總數約兩百多個,涵蓋了 Android 作業系統 API 的各個層面:

  • App:Activity 生命週期、Dialog、Menu、Notification、Preferences、Search、Service、Alarm、Intent、Device Admin、Text-To-Speech、語音辨識等
  • Content:Assets 讀取、Resources 與 Styled Text、外部儲存空間
  • Graphics:Bitmap 相關操作、Canvas 繪圖、Path 效果、Drawable、觸控繪圖、Unicode 字元表等
  • Media:MediaPlayer、VideoView
  • OS:Sensors、SMS Messaging、Morse Code
  • Text:Linkify、LogTextBox、Marquee
  • Views:Layout 家族、Lists、Grid、Gallery、Tabs、Animation、Auto Complete、Progress Bar、Seek Bar 等

App 類:最值得優先閱讀的部分

Activity 相關的範例特別密集,包含 Animation、Custom Dialog、Custom Title、Dialog、Forwarding、Hello World、Persistent State、Receive Result、Redirection、Reorder Activities、Save & Restore State、SetWallpaper、Translucent 與 Translucent Blur、Wallpaper 等,基本把 Activity 各種啟動模式與狀態處理都示範過了。

Service 系列分成 Local 與 Remote 兩條線,各自又有 Binding 與 Controller 的組合,加上 Foreground Service、Messenger Service、Service Start Arguments,可以說是理解 Service 這個元件最完整的教材。此外 Notification 四例(IncomingMessage、Notifying Service Controller、NotifyWithText、Status Bar)與 Preferences 六例(從 XML 宣告到程式碼建立、進階用法)也都值得逐一動手跑。

Views 類:Layout 是重頭戲

Layouts 底下的 Baseline、LinearLayout(10 個變體)、RelativeLayout、ScrollView、TableLayout(12 個變體)構成了一套循序漸進的版面教學,從最基本的垂直排列一路到 Layout Weight、Gravity、Column Collapse、Cell Spanning。Lists 也有 14 個範例,特別是 Slow Adapter 與 Efficient Adapter 的對比,直接說明了 ListView 效能差異的來源。

Android 4.x 新增的內容

4.x 版本重新整理並擴充了 ApiDemos,新增了幾個重要的分類:

Accessibility

Node Querying、Accessibility Service、Custom View 三例,示範了如何讓 App 對輔助功能友善。

Animation

這是新版新增的重頭戲,共 13 例:Bouncing Balls、Cloning、Custom Evaluator、Default Layout Animations、Events、Hide-Show Animations、Layout Animations、Loading、Multiple Properties、Reversing、Seeking、View Flip,完整涵蓋了 property animation 的各種能力。

App 的新增部分

Action Bar 相關(Mechanics、Tabs、Usage、Settings Action Provider、Share Action Provider、Display Options)、Fragment 系列 17 例(含 Dialog、List Array、Retain Instance、Stack、Tabs 等)、Loader 三例(Cursor、Custom、Throttle)都是 4.x 才有的重要主題。

其他新分類

Content 增加了 Clipboard 與 Packages;Graphics 增加了完整的 OpenGL ES 子分類(含 GLSurfaceView、Cube Map、Frame Buffer Object、Matrix Palette Skinning、OpenGL ES 2.0 等);Media 加入 AudioFx;此外還有全新的 NFC 分類(ForegroundDispatch、ForegroundNdefPush、TechFilter),以及 Preference 的 Fragment、Headers、Switch 等新範例。

怎麼用這份索引

ApiDemos 的價值不在「照抄」,而在於它提供了一個可執行的 API 百科:遇到不熟的元件,先找到對應範例跑一次、看它怎麼宣告與呼叫,往往比讀文件更快進入狀況。建議的閱讀順序是:先 App/Activity 建立基本骨架感,接著 Views 把版面吃透,然後視需求深入 Service、Graphics 或 NFC。

後續將會陸續補充 Android 4.x 新增範例的逐篇解析。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved