自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android開發(fā)速成簡(jiǎn)潔教程九:創(chuàng)建應(yīng)用程序框架

移動(dòng)開發(fā) Android
Android簡(jiǎn)明開發(fā)教程八說(shuō)明了程序需要實(shí)現(xiàn)的功能,就可以創(chuàng)建Android項(xiàng)目了。請(qǐng)參見Android簡(jiǎn)明開發(fā)教程三:第一個(gè)應(yīng)用Hello World ,創(chuàng)建一個(gè)新項(xiàng)目AndroidGraphics2DTutorial。今天先介紹創(chuàng)建的程序的框架。

Android簡(jiǎn)明開發(fā)教程八說(shuō)明了程序需要實(shí)現(xiàn)的功能,就可以創(chuàng)建Android項(xiàng)目了。請(qǐng)參見Android簡(jiǎn)明開發(fā)教程三:第一個(gè)應(yīng)用Hello World ,創(chuàng)建一個(gè)新項(xiàng)目AndroidGraphics2DTutorial。今天先介紹創(chuàng)建的程序的框架。然后再項(xiàng)目添加如下類定義:

添加第三方庫(kù)文件

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

類說(shuō)明,下表列出了項(xiàng)目中定義的類的簡(jiǎn)要說(shuō)明:

說(shuō)明
AndroidGraphics2DApplication 應(yīng)用程序類,為Application子類
AndroidGraphics2DTutorial 主Activity,為L(zhǎng)istActivity子類,用于列出其它示例。
GuidebeeGraphics2DSurfaceView SurfaceView子類用于顯示圖形
GuidebeeGraphics2DView View子類用于顯示圖形,與GuidebeeGraphics2DSurfaceView 功能一樣,在程序中可以互換。
SharedGraphics2DInstance 定義了共享類對(duì)象,主要包含Graphics2D
Graphics2DActivity Activity子類,為所有示例基類,定義一些所有示例共享的類變量和函數(shù)。
Bezier,Brush,Colors,F(xiàn)ont,Image,Path,Pen,Shape,Transform 為Graphics2DActivity的子類,為二維圖形演示各個(gè)功能

AndroidGraphics2DApplication ,其實(shí)在一般的Android應(yīng)用中,無(wú)需定義Application的派生類,比如在Hello World中就沒(méi)有定義,當(dāng)是如果想在多個(gè)Activity中共享變量,或是想初始化一些全局變量,可以定義Application的派生類,然后可以在 Activity或Service中調(diào)用 getApplication() 或 getApplicationContext()來(lái)取得Application 對(duì)象,可以訪問(wèn)定義在Application中的一些共享變量。在這個(gè)例子中AndroidGraphics2DApplication嚴(yán)格些也可不定 義,為了說(shuō)明問(wèn)題,還是定義了用來(lái)初始化Graphics2D實(shí)例,Graphics2D實(shí)例可以被所有示例Activity,如Colors,F(xiàn)ont 訪問(wèn)。如果定義了Application的派生類,就需要在AndroidManifest.xml中說(shuō)明Application派生類的位置。

  1. <manifest xmlns:android=”http://schemas.android.com/apk/res/android” 
  2.       package=”com.pstreets.graphics2d” 
  3.       android:versionCode=”1″ 
  4.       android:versionName=”1.0″> 
  5.     <application android:name=”AndroidGraphics2DApplication” 
  6.          android:icon=”@drawable/icon” android:label=”@string/app_name”> 
  7.         <activity android:name=”.AndroidGraphics2DTutorial” 
  8.                   android:label=”@string/app_name”> 
  9.             <intent-filter> 
  10.                 <action android:name=”android.intent.action.MAIN” /> 
  11.                 <category android:name=”android.intent.category.LAUNCHER” /> 
  12.             </intent-filter> 
  13.         </activity> 
  14.   … 
  15.     </application> 
  16.     <uses-sdk android:minSdkVersion=”4″ /> 
  17.  
  18. </manifest>    

Application 可以重載 onCreate()和 onTerminate() ,onCreate()在應(yīng)用啟動(dòng)時(shí)執(zhí)行一次,onTerminate()在應(yīng)用推出執(zhí)行一次。 AndroidGraphics2DApplication 的onCreate() 中初始化Graphics2D實(shí)例:

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

AndroidGraphics2DTutorial 為L(zhǎng)istActivity子類,直接從AndroidManifest.xml中讀取Intent-Filter Catetory 為 com.pstreets.graphics2d.SAMPLE_CODE 的所有Activity。

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

GuidebeeGraphics2DSurfaceView 和 GuidebeeGraphics2DView 分別為SurfaceView 和View的子類,都可以用來(lái)顯示圖形結(jié)果。在程序中可以互換。

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

SurfaceView 動(dòng)態(tài)顯示性能比較好,一般用在游戲畫面的顯示。圖形的繪制可以在單獨(dú)的線程中完成。

修改 res\layout\main.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?> 
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
  3.     android:orientation=”vertical” 
  4.     android:layout_width=”fill_parent” 
  5.     android:layout_height=”fill_parent” 
  6.     > 
  7. <com.pstreets.graphics2d.GuidebeeGraphics2DSurfaceView 
  8.      android:id=”@+id/graphics2dview” 
  9.    
  10.      android:layout_width=”fill_parent” 
  11.      android:layout_height=”fill_parent” /> 
  12. </LinearLayout> 

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

為了能在AndroidGraphics2DTutorial 列表中列出,對(duì)項(xiàng)目中的示例Activity的都定義下列intent-filter

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

這樣就完成了程序框架的設(shè)計(jì),起始界面如下:

責(zé)任編輯:閆佳明 來(lái)源: imobilebbs
相關(guān)推薦

2013-12-26 15:43:07

Android開發(fā)Android應(yīng)用Activities

2013-12-26 15:10:08

Android開發(fā)應(yīng)用和框架Linux 內(nèi)核

2013-12-26 15:34:19

Android開發(fā)Android應(yīng)用基本概念

2013-12-27 16:06:10

Android開發(fā)Android應(yīng)用發(fā)布應(yīng)用

2013-12-26 15:18:09

Android開發(fā)安裝開發(fā)環(huán)境

2013-12-27 14:34:46

Android開發(fā)Android應(yīng)用短信觸發(fā)示例

2013-12-27 14:05:22

Android開發(fā)Android應(yīng)用Dialog

2013-12-27 14:16:43

Android開發(fā)Android應(yīng)用線程

2013-12-27 13:49:22

Android開發(fā)Android應(yīng)用Button

2013-12-26 16:59:12

Android開發(fā)Android應(yīng)用數(shù)據(jù)綁定Data Bi

2013-12-27 12:51:44

Android開發(fā)Android應(yīng)用引路蜂

2013-12-26 16:24:13

Android開發(fā)Android應(yīng)用Intents

2013-12-27 13:27:05

Android開發(fā)Android應(yīng)用RadioButton

2013-12-26 15:46:30

Android開發(fā)Android應(yīng)用用戶界面設(shè)計(jì)

2013-12-26 15:25:15

Android開發(fā)安裝開發(fā)環(huán)境Hello World

2013-12-27 15:31:26

Android開發(fā)Android應(yīng)用資源Resources

2022-09-27 15:16:42

開發(fā)Android應(yīng)用程序

2013-12-26 17:08:36

Android開發(fā)Android應(yīng)用自定義Adapter顯

2013-12-27 13:00:30

Android開發(fā)Android應(yīng)用Context Men

2024-09-06 10:46:04

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)