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

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

Android简明开发教程八说明了程序需要实现的功能,就可以创建Android项目了。请参见Android简明开发教程三:第一个应用Hello World ,创建一个新项目AndroidGraphics2DTutorial。今天先介绍创建的程序的框架。然后再项目添加如下类定义:

添加第三方库文件

AndroidGraphics2DTutorial调用了引路蜂二维图形库,因此需要在项目中添加第三方库引用(libgisengine.jar),打开Android属性窗口,添加External JARs。把libgisengine.jar 添加到项目中,引路蜂二维图形库是引路蜂地图开发包的一部分。添加库引用可以参见 Android引路蜂地图开发示例:基本知识

类说明,下表列出了项目中定义的类的简要说明:

说明
AndroidGraphics2DApplication应用程序类,为Application子类
AndroidGraphics2DTutorial主Activity,为ListActivity子类,用于列出其它示例。
GuidebeeGraphics2DSurfaceViewSurfaceView子类用于显示图形
GuidebeeGraphics2DViewView子类用于显示图形,与GuidebeeGraphics2DSurfaceView 功能一样,在程序中可以互换。
SharedGraphics2DInstance定义了共享类对象,主要包含Graphics2D
Graphics2DActivityActivity子类,为所有示例基类,定义一些所有示例共享的类变量和函数。
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值