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

来源:互联网 发布:淘宝开店认证 上半身 编辑:程序博客网 时间:2024/06/04 17:53

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定义了共享类对象,主要包含Graphics2DGraphics2DActivityActivity子类,为所有示例基类,定义一些所有示例共享的类变量和函数。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=
      newGraphics2D(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 extendsView {
 
 publicGuidebeeGraphics2DView(Context context, AttributeSet attrs,
   intdefStyle) {
  super(context, attrs, defStyle);
 
 }
 
 publicGuidebeeGraphics2DView(Context context, AttributeSet attrs) {
  super(context, attrs);
 
 }
 
 publicGuidebeeGraphics2DView(Context context) {
  super(context);
 
 }
 
 publicvoid onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawColor(0xFFFFFFFF);
  if(SharedGraphics2DInstance.graphics2d != null) {
   intoffsetX = (getWidth() -
     SharedGraphics2DInstance.CANVAS_WIDTH) /2;
   intoffsetY = (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
   SurfaceViewimplements SurfaceHolder.Callback {
 
 SurfaceHolder holder;
 
 privatevoid initHolder() {
  holder =this.getHolder();
  holder.addCallback(this);
 }
 
 publicGuidebeeGraphics2DSurfaceView(Context context,
   AttributeSet attrs,
   intdefStyle) {
  super(context, attrs, defStyle);
  initHolder();
 
 }
 
 publicGuidebeeGraphics2DSurfaceView(Context context,
   AttributeSet attrs) {
  super(context, attrs);
  initHolder();
 
 }
 
 publicGuidebeeGraphics2DSurfaceView(Context context) {
  super(context);
  initHolder();
 
 }
 
 @Override
 publicvoid surfaceChanged(SurfaceHolder arg0,
   intarg1, int arg2, int arg3) {
  // TODO Auto-generated method stub
 
 }
 
 @Override
 publicvoid surfaceCreated(SurfaceHolder arg0) {
  newThread(new MyThread()).start();
 
 }
 
 @Override
 publicvoid surfaceDestroyed(SurfaceHolder arg0) {
  // TODO Auto-generated method stub
 
 }
 
 classMyThread implementsRunnable {
 
  @Override
  publicvoid run() {
   Canvas canvas = holder.lockCanvas(null);
   canvas.drawColor(0xFFFFFFFF);
   if(SharedGraphics2DInstance.graphics2d != null) {
    intoffsetX = (getWidth() -
      SharedGraphics2DInstance.CANVAS_WIDTH) /2;
    intoffsetY = (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>

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


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 自来水被农药水污染了怎么办 雾霾天头疼恶心怎么办? 夫妻住宾馆一个没有身份证怎么办 医保报销后认定工伤怎么办 结肠癌术后复查有息肉怎么办 无蒂息肉恶变要怎么办 贤者时间很长怎么办 鸡吃了酒米醉了怎么办 自填脂肪乳房脂肪液化怎么办 中国人在外国遇到危险怎么办 dnf刷图卡住了怎么办 dbf深渊怪卡住了怎么办 dnf86级没任务了怎么办 dnf二觉任务没了怎么办 脚趾甲变空向上翘怎么办 汽油车加了一点柴油怎么办 柴油车辆环保检测功率不足怎么办 加95加错一次92怎么办 新车95加错92油怎么办 加不到95号汽油怎么办 去新疆没95号油怎么办 黄龙300加了92怎么办 gla错加92号油 怎么办 95和98混加了怎么办 沥青车可以停在居民区怎么办 汽油进到眼睛了怎么办 汽油进了眼睛里怎么办 眼睛里面进了汽油怎么办 脱硫塔里的二氧化硫高怎么办 恐怖黎明铁匠选错怎么办 堡垒之夜草变色怎么办 火柴没有擦的了怎么办 乙醚倒进下水道了怎么办 乙醚和水不分层怎么办 乙醚闻多了头晕怎么办 爱乐维吃了便秘怎么办 刮完滑石粉墙面很软怎么办 被硫酸泼到皮肤怎么办 头磕了一下头晕怎么办 家里有事与工作不能请怎么办 撞了头头晕想吐怎么办