在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 創(chuàng)建應(yīng)用程序框架
Dialog 顯示圖像
線程 Bezier 曲線
創(chuàng)建應(yīng)用程序框架
引路蜂二維圖形庫簡介及顏色示例
Android 應(yīng)用基本概念
Intents 和 Intent Filters
安裝開發(fā)環(huán)境
Option Menu 畫筆示例
自定義對(duì)話框 Transform
數(shù)據(jù)綁定 Data Binding
概述
Broadcast Receiver 短信觸發(fā)示例
發(fā)布應(yīng)用
自定義 Adapter 顯示列表
RadioButton 多邊形及路徑繪制
訪問 Internet 繪製在線地圖
第一個(gè)應(yīng)用 Hello World
Activities
Button 畫刷示例
使用資源 Resources
Context Menu 繪制幾何圖形
用戶界面設(shè)計(jì)
引路蜂二維圖形繪制實(shí)例功能定義

創(chuàng)建應(yīng)用程序框架

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

http://wiki.jikexueyuan.com/project/android-development-tutorial/images/17.png" alt="" />

添加第三方庫文件

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

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

說明
AndroidGraphics2DApplication 應(yīng)用程序類,為Application子類
AndroidGraphics2DTutorial 主Activity,為ListActivity子類,用于列出其它示例。
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)用中,無需定義 Application 的派生類,比如在 Hello World 中就沒有定義,當(dāng)是如果想在多個(gè) Activity 中共享變量,或是想初始化一些全局變量,可以定義 Application 的派生類,然后可以在 Activity 或 Service 中調(diào)用 getApplication() 或 getApplicationContext()來取得 Application 對(duì)象,可以訪問定義在Application 中的一些共享變量。在這個(gè)例子中 AndroidGraphics2DApplication 嚴(yán)格些也可不定義,為了說明問題,還是定義了用來初始化 Graphics2D 實(shí)例,Graphics2D 實(shí)例可以被所有示例Activity,如 Colors,F(xiàn)ont 訪問。如果定義了 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()在應(yīng)用啟動(dòng)時(shí)執(zhí)行一次,onTerminate()在應(yīng)用推出執(zhí)行一次。AndroidGraphics2DApplication 的 onCreate() 中初始化Graphics2D 實(shí)例:

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。

private static final String SAMPLE_CATEGORY="com.pstreets.graphics2d.SAMPLE_CODE";

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(SAMPLE_CATEGORY);
...

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

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的子類,都可以用來顯示圖形結(jié)果。在程序中可以互換。

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);
  }
 }

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

修改 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 列表中列出,對(duì)項(xiàng)目中的示例 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>

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

http://wiki.jikexueyuan.com/project/android-development-tutorial/images/18.png" alt="" />

Tags: Android