Android簡明開發教程九:創建應用程序框架

Android簡明開發教程九:創建應用程序框架

Android簡明開發教程八說明了程序需要實現的功能,就可以創建Android項目了。請參見Android簡明開發教程三:第一個應用Hello World ,創建一個新項目AndroidGraphics2DTutorial。今天先介紹創建的程序的框架。然後再項目添加如下類定義:

添加第三方庫文件

AndroidGraphics2DTutorial調用了引路蜂二維圖形庫,因此需要在項目中添加第三方庫引用(libgisengine.jar),打開Android屬性窗口,添加External JARs。把libgisengine.jar 添加到項目中,引路蜂二維圖形庫是引路蜂地圖開發包的一部分。添加庫引用可以參見 Android引路蜂地圖開發示例:基本知識

類說明,下表列出了項目中定義的類的簡要說明:

說明
AndroidGraphics2DApplication 應用程序類,爲Application子類
AndroidGraphics2DTutorial 主Activity,爲ListActivity子類,用於列出其它示例。
GuidebeeGraphics2DSurfaceView SurfaceView子類用於顯示圖形
GuidebeeGraphics2DView View子類用於顯示圖形,與GuidebeeGraphics2DSurfaceView 功能一樣,在程序中可以互換。
SharedGraphics2DInstance 定義了共享類對象,主要包含Graphics2D
Graphics2DActivity Activity子類,爲所有示例基類,定義一些所有示例共享的類變量和函數。
Bezier,Brush,Colors,Font,Image,Path,Pen,Shape,Transform 爲Graphics2DActivity的子類,爲二維圖形演示各個功能

AndroidGraphics2DApplication ,其實在一般的Android應用中,無需定義Application的派生類,比如在Hello World中就沒有定義,當是如果想在多個Activity中共享變量,或是想初始化一些全局變量,可以定義Application的派生類,然後可以在Activity或Service中調用 getApplication() 或 getApplicationContext()來取得Application 對象,可以訪問定義在Application中的一些共享變量。在這個例子中AndroidGraphics2DApplication嚴格些也可不定義,爲了說明問題,還是定義了用來初始化Graphics2D實例,Graphics2D實例可以被所有示例Activity,如Colors,Font訪問。如果定義了Application的派生類,就需要在AndroidManifest.xml中說明Application派生類的位置。

<manifest xmlns:android=”http://schemas.android.com/apk/res/android
      package=”com.pstreets.graphics2d
      android:versionCode=”1″
      android:versionName=”1.0″>
    <application android:name=”AndroidGraphics2DApplication
         android:icon=”@drawable/icon” android:label=”@string/app_name”>
        <activity android:name=”.AndroidGraphics2DTutorial”
                  android:label=”@string/app_name”>
            <intent-filter>
                <action android:name=”android.intent.action.MAIN” />
                <category android:name=”android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>
  …
    </application>
    <uses-sdk android:minSdkVersion=”4″ />

</manifest>   

Application 可以重載 onCreate()和 onTerminate() ,onCreate()在應用啓動時執行一次,onTerminate()在應用推出執行一次。AndroidGraphics2DApplication 的onCreate() 中初始化Graphics2D實例:

1
2
3
4
5
public void onCreate() {
  SharedGraphics2DInstance.graphics2d=
      new Graphics2D(SharedGraphics2DInstance.CANVAS_WIDTH,
        SharedGraphics2DInstance.CANVAS_HEIGHT);
 }

AndroidGraphics2DTutorial 爲ListActivity子類,直接從AndroidManifest.xml中讀取Intent-Filter Catetory 爲 com.pstreets.graphics2d.SAMPLE_CODE 的所有Activity。

1
2
3
4
5
private static final String SAMPLE_CATEGORY="com.pstreets.graphics2d.SAMPLE_CODE";
 
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(SAMPLE_CATEGORY);
...

GuidebeeGraphics2DSurfaceView 和 GuidebeeGraphics2DView 分別爲SurfaceView 和View的子類,都可以用來顯示圖形結果。在程序中可以互換。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.pstreets.graphics2d;
 
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
 
public class GuidebeeGraphics2DView extends View {
 
 public GuidebeeGraphics2DView(Context context, AttributeSet attrs,
   int defStyle) {
  super(context, attrs, defStyle);
 
 }
 
 public GuidebeeGraphics2DView(Context context, AttributeSet attrs) {
  super(context, attrs);
 
 }
 
 public GuidebeeGraphics2DView(Context context) {
  super(context);
 
 }
 
 public void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawColor(0xFFFFFFFF);
  if (SharedGraphics2DInstance.graphics2d != null) {
   int offsetX = (getWidth() -
     SharedGraphics2DInstance.CANVAS_WIDTH) / 2;
   int offsetY = (getHeight()
     - SharedGraphics2DInstance.CANVAS_HEIGHT) / 2;
   canvas.drawBitmap(SharedGraphics2DInstance.graphics2d.getRGB(), 0,
     SharedGraphics2DInstance.CANVAS_WIDTH,
     offsetX, offsetY,
     SharedGraphics2DInstance.CANVAS_WIDTH,
     SharedGraphics2DInstance.CANVAS_HEIGHT,
     true, null);
  }
 }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.pstreets.graphics2d;
 
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 
public class GuidebeeGraphics2DSurfaceView extends
   SurfaceView implements SurfaceHolder.Callback {
 
 SurfaceHolder holder;
 
 private void initHolder() {
  holder = this.getHolder();
  holder.addCallback(this);
 }
 
 public GuidebeeGraphics2DSurfaceView(Context context,
   AttributeSet attrs,
   int defStyle) {
  super(context, attrs, defStyle);
  initHolder();
 
 }
 
 public GuidebeeGraphics2DSurfaceView(Context context,
   AttributeSet attrs) {
  super(context, attrs);
  initHolder();
 
 }
 
 public GuidebeeGraphics2DSurfaceView(Context context) {
  super(context);
  initHolder();
 
 }
 
 @Override
 public void surfaceChanged(SurfaceHolder arg0,
   int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub
 
 }
 
 @Override
 public void surfaceCreated(SurfaceHolder arg0) {
  new Thread(new MyThread()).start();
 
 }
 
 @Override
 public void surfaceDestroyed(SurfaceHolder arg0) {
  // TODO Auto-generated method stub
 
 }
 
 class MyThread implements Runnable {
 
  @Override
  public void run() {
   Canvas canvas = holder.lockCanvas(null);
   canvas.drawColor(0xFFFFFFFF);
   if (SharedGraphics2DInstance.graphics2d != null) {
    int offsetX = (getWidth() -
      SharedGraphics2DInstance.CANVAS_WIDTH) / 2;
    int offsetY = (getHeight() -
      SharedGraphics2DInstance.CANVAS_HEIGHT) / 2;
    canvas.drawBitmap
      (SharedGraphics2DInstance.graphics2d.getRGB(),
      0, SharedGraphics2DInstance.CANVAS_WIDTH,
      offsetX,
      offsetY,
      SharedGraphics2DInstance.CANVAS_WIDTH,
      SharedGraphics2DInstance.CANVAS_HEIGHT,
      true, null);
   }
   holder.unlockCanvasAndPost(canvas);
 
  }
 
 }
 
}

SurfaceView 動態顯示性能比較好,一般用在遊戲畫面的顯示。圖形的繪製可以在單獨的線程中完成。

修改 res\layout\main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android
    android:orientation=”vertical”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    >
<com.pstreets.graphics2d.GuidebeeGraphics2DSurfaceView
     android:id=”@+id/graphics2dview”
  
     android:layout_width=”fill_parent”
     android:layout_height=”fill_parent” />
</LinearLayout>

如果使用 GuidebeeGraphics2DView作爲顯示,則只需將上面紅色部分該成GuidebeeGraphics2DView即可。

爲了能在AndroidGraphics2DTutorial 列表中列出,對項目中的示例Activity的都定義下列intent-filter

<activity android:name=”.example.Colors” android:label=”@string/activity_colors”>
            <intent-filter>
                <action android:name=”android.intent.action.MAIN” />
                <category android:name=”com.pstreets.graphics2d.SAMPLE_CODE” />
            </intent-filter>
        </activity>

這樣就完成了程序框架的設計,起始界面如下:


發佈了0 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章