、プログラムが何を達成する必要があるかを説明し、Androidプロジェクトを作成する時間です。新しいプロジェクトAndroidGraphics2DTutorialを作成するには、 参照してください。次に、以下のクラス定義をプロジェクトに追加します:
サードパーティライブラリファイルの追加
AndroidGraphics2DTutorialは、LeadBee 2Dグラフィックス・ライブラリを呼び出すので、プロジェクトにサードパーティ・ライブラリへの参照を追加する必要があります。ライブラリ・リファレンスを追加するには、Android LeadBee Map Development Example: Basicsを参照してください。
クラスの説明では、プロジェクトで定義されているクラスの簡単な説明を以下の表に示します:
AndroidGraphics2DApplication 、実は一般的なAndroidアプリケーションでは、Applicationの派生クラスを定義する必要はありません。例えば、Hello Worldでは定義されていませんが、複数のActivityで変数を共有したい場合や、いくつかのグローバル変数を初期化したい場合は、Applicationの派生クラスを定義します。複数のActivityで変数を共有したり、グローバル変数を初期化したい場合は、Applicationの派生クラスを定義し、ActivityやServiceでgetApplication()やgetApplicationContext()を呼び出してApplicationオブジェクトを取得することで、Applicationで定義された共有変数にアクセスすることができます。この例では、AndroidGraphics2DApplicationは厳密かどうかは分かりませんが、説明のためにGraphics2Dのインスタンスを初期化するために定義しています。Applicationの派生クラスを定義する場合は、AndroidManifest.xmlでApplicationの派生クラスの場所を指定する必要があります。
<manifest xmlns:android=”http://..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>
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として直接読み込みます。アクティビティ
private static final String SAMPLE_CATEGORY="com.pstreets.graphics2d.SAMPLE_CODE";
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(SAMPLE_CATEGORY);
...
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の動的表示性能の方が優れており、通常はゲーム画面の表示に使用されます。グラフィックの描画は別のスレッドで行うことができます。
reslayoutの修正
<?xml version=”1.0 encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://..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>
<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>
これでプログラムのフレームワークの設計は完了:





