Android简明开发教程九:创建应用程序框架

jerry Android 2015年08月24日 收藏

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. public void onCreate() {
  2.   SharedGraphics2DInstance.graphics2d=
  3.       new Graphics2D(SharedGraphics2DInstance.CANVAS_WIDTH,
  4.         SharedGraphics2DInstance.CANVAS_HEIGHT);
  5.  }

AndroidGraphics2DTutorial 为ListActivity子类,直接从AndroidManifest.xml中读取Intent-Filter Catetory 为 com.pstreets.graphics2d.SAMPLE_CODE 的所有Activity。

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

GuidebeeGraphics2DSurfaceView 和 GuidebeeGraphics2DView 分别为SurfaceView 和View的子类,都可以用来显示图形结果。在程序中可以互换。

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

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>

这样就完成了程序框架的设计,起始界面如下: