Example-LunarLand.ppt

420 views
329 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
420
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Example-LunarLand.ppt

  1. 1. Android 程式範例 : LunerLand 登陸月球小遊戲
  2. 2. LunerLand 專案結構
  3. 3. LunerLand 專案的結構 <ul><li>src/ : 原始程式碼 </li></ul><ul><ul><li>LunarLander.java , LunerView.java </li></ul></ul><ul><ul><li>R.java : 自動產生的資源常數檔 , 對映 res/ 中的項目 . </li></ul></ul><ul><li>Referenced Librarys </li></ul><ul><ul><li>引用函式庫 , 通常為 Android.jar </li></ul></ul><ul><li>assets/ : 資產 </li></ul><ul><ul><li>??? </li></ul></ul><ul><li>res/ : 資源 </li></ul><ul><ul><li>包含 drawable, layout, value 等資源 . </li></ul></ul><ul><li>AndroidManifest.xml : 清單 </li></ul><ul><ul><li>用來結合所有程式形成一個應用架構 </li></ul></ul>
  4. 4. Android Manifest <manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; package=&quot;com.google.android.lunarlander&quot;> <application android:icon=&quot;@drawable/app_lunar_lander&quot; android:label=&quot;@string/app_name&quot;> <activity class=&quot;LunarLander&quot;> <intent-filter> <action android:value=&quot;android.intent.action.MAIN&quot; /> <category android:value=&quot;android.intent.category.LAUNCHER&quot; /> </intent-filter> </activity> </application> </manifest>
  5. 5. R.java package com.google.android.lunarlander; public final class R { public static final class attr { } public static final class drawable { public static final int app_lunar_lander=0x7f020000; public static final int earthrise=0x7f020001; public static final int lander_crashed=0x7f020002; public static final int lander_firing=0x7f020003; public static final int lander_plain=0x7f020004; } public static final class id { public static final int lunar=0x7f050000; public static final int text=0x7f050001; } public static final class layout { public static final int lunar_layout=0x7f030000; }
  6. 6. R.java public static final class string { public static final int app_name=0x7f040000; public static final int lunar_layout_text_text=0x7f040011; public static final int menu_easy=0x7f040005; public static final int menu_hard=0x7f040007; public static final int menu_medium=0x7f040006; public static final int menu_pause=0x7f040003; public static final int menu_resume=0x7f040004; public static final int menu_start=0x7f040001; public static final int menu_stop=0x7f040002; public static final int message_bad_angle=0x7f040010; public static final int message_off_pad=0x7f04000e; public static final int message_stopped=0x7f04000d; public static final int message_too_fast=0x7f04000f; public static final int mode_lose=0x7f04000a; public static final int mode_pause=0x7f040009; public static final int mode_ready=0x7f040008; public static final int mode_win_prefix=0x7f04000b; public static final int mode_win_suffix=0x7f04000c; } }
  7. 7. res/drawable/ drawable-land/earthride.png app_lunar_lander.png lander_crashed.png lander_firing.png lander_plain.png
  8. 8. res/values/string.xml <resources> <string name=&quot;app_name&quot;>Lunar Lander</string> <string name=&quot;menu_start&quot;>Start</string> <string name=&quot;menu_stop&quot;>Stop</string> <string name=&quot;menu_pause&quot;>Pause</string> <string name=&quot;menu_resume&quot;>Resume</string> <string name=&quot;menu_easy&quot;>Easy</string> <string name=&quot;menu_medium&quot;>Medium</string> <string name=&quot;menu_hard&quot;>Hard</string> <string name=&quot;mode_ready&quot;>Lunar Lander Press Up To Play</string> <string name=&quot;mode_pause&quot;>Paused Press Up To Resume</string> <string name=&quot;mode_lose&quot;>Game Over Press Up To Play</string> <string name=&quot;mode_win_prefix&quot;>Success! </string> <string name=&quot;mode_win_suffix&quot;>in a row Press Up to Play</string> <string name=&quot;message_stopped&quot;>Stopped</string> <string name=&quot;message_off_pad&quot;>Off Landing Pad</string> <string name=&quot;message_too_fast&quot;>Too Fast</string> <string name=&quot;message_bad_angle&quot;>Bad Angle</string> <string name=&quot;lunar_layout_text_text&quot;></string> </resources>
  9. 9. LunerLander.java – 主程式 <ul><li>LunerLander class 是一個 Activity </li></ul><ul><ul><li>使用 LunerView 作為 ContentView </li></ul></ul><ul><ul><li>只負責 Activity 的生命周期相關事務 </li></ul></ul>
  10. 10. LunerView.java – 遊戲畫面 <ul><li>注意 : </li></ul><ul><ul><li>L unerView 並沒有採用時鐘驅動的概念,而是在 onDraw 中使用不斷 invalidate 的方式,來進行移動重畫的工作。 </li></ul></ul>
  11. 11. LunerView 畫圖 <ul><li>void onDraw(Canvas canvas) </li></ul><ul><ul><li>mLanderImage.setBounds(xLeft, yTop, xLeft+mLanderWidth, yTop+mLanderHeight); </li></ul></ul><ul><ul><li>mLanderImage.draw(canvas); </li></ul></ul><ul><ul><li>if (mMode == RUNNING) { </li></ul></ul><ul><ul><ul><li>invalidate(xLeft-20, yTop-20, Left+mLanderWidth+20, Top+mLanderHeight+20); </li></ul></ul></ul><ul><ul><ul><li>invalidate(0, 0, screenWidth, 4 + BAR_HEIGHT); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 12. OnActivityResult() <ul><li>Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startSubActivity(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, String, Bundle) method. </li></ul><ul><li>When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER </li></ul>
  13. 13. onActivityResult() 的範例 <ul><li>public class MyActivity extends Activity  {     ...      static final int PICK_CONTACT_REQUEST = 0;      protected boolean onKeyDown(int keyCode, KeyEvent event)      {          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {              // When the user center presses, let them pick a contact.              startSubActivity(                  new Intent(Intent.PICK_ACTION, new ContentURI(&quot;content://contacts&quot;)), PICK_CONTACT_REQUEST);             return true;          }          return false;      }      protected void onActivityResult(int requestCode, int resultCode,  String data, Bundle extras)      {          if (requestCode == PICK_CONTACT_REQUEST) {              if (resultCode == RESULT_OK) {                  // A contact was picked.  Here we will just display it to the user.                  startActivity(new Intent(Intent.VIEW_ACTION, data));              }          }      }  }   </li></ul>
  14. 14. Intent <ul><li>Intent ( Context packageContext, Class cls) </li></ul><ul><ul><li>Create an intent for a specific component. </li></ul></ul><ul><li>Intent ( String action, ContentURI uri) </li></ul><ul><ul><li>Create an intent with a given action and for a given data url. </li></ul></ul>
  15. 15. DBHelper.java
  16. 16. NoteEdit.java
  17. 17. Activity 的流程概念 <ul><li>注意 </li></ul><ul><ul><li>onPause 時行程會被置換出去 </li></ul></ul><ul><ul><li>onResume 時會再度被置換回來 </li></ul></ul><ul><ul><li>有可能會被 kill 掉,因此要先記錄資訊 </li></ul></ul>
  18. 18. startActivity, onActivityResult <ul><li>The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent , which describes the activity to be executed. </li></ul><ul><li>Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startSubActivity(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, String, Bundle) method. </li></ul>

×