むずかしいことはわかりません

いろいろ書いてるみたいな

SurfaceView

ゲームのようにリアルタイムな用途にはSurfaceViewが良いと聞いて、じゃあ、と色々調べながら書き直したんですがどうにもタッチイベントとか拾ってると処理落ちするのは仕様でしょうか?作り方が悪いんだろうか…。

とりあえず、タスク用、キャラ表示用のクラスはできたので、描画優先付けられるようなものを作ります。(うろおぼえだけど、昔某機で使ったDrawPrimitiveみたいなの)


その前に、lunarlanderのソース見てくるか…。

※7/3追記
SurfaceViewで検索すると上の方に出てくるみたいなので…いろいろ寄せ集めて作った試作版を参考になるかわかりませんが載せておきます。もっと良いのがあったら、トラックバックかコメントで教えてくださいw

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.view.*;
import android.graphics.*;
import android.content.*;
import android.os.*;

public class mainscreen extends SurfaceView implements SurfaceHolder.Callback {

	public static SurfaceHolder holderMainView;
	public static Context		contextMainView;

	private int touchX = 0;
	private int touchY = 0;
	private int touchAction = 0;

	private Paint localPaint = new Paint();


	public mainscreen(Context context) {
		super(context);
		// TODO 自動生成されたコンストラクター・スタブ
		contextMainView = context;
		holderMainView = getHolder();
		holderMainView.addCallback(this);
		holderMainView.setFixedSize(getWidth(), getHeight());
	}

	private void drawAll(Canvas canvas ) {
		super.onDraw(canvas);

		localPaint.setColor(Color.WHITE);
		localPaint.setAntiAlias(true);
		localPaint.setTextSize(24);

		canvas.drawText("Touch("+touchX+","+touchY+")", 0, 5 + 26 * 1, localPaint);
		if ( touchAction == 1 ) {
			canvas.drawText("Touch > Hit!!", 0, 5 + 26 * 2, localPaint);
		} else {
			canvas.drawText("Touch > ", 0, 5 + 26 * 2, localPaint);
		}
	}

	public boolean onKeyDown( int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		touchX = (int)event.getX();
		touchY = (int)event.getY();

		touchAction = 0;

		switch ( event.getAction() ) {
		case	MotionEvent.ACTION_MOVE:
		case	MotionEvent.ACTION_DOWN:
		case	MotionEvent.ACTION_UP:
		case	MotionEvent.ACTION_CANCEL:
		default:
			break;
		}

		return true;
	}

	public void resizeSurfaceSize ( int width , int height ) {
		synchronized (holderMainView) {
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO 自動生成されたメソッド・スタブ
		resizeSurfaceSize ( width , height );
	}

	ScheduledExecutorService executor;

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO 自動生成されたメソッド・スタブ
		executor = Executors.newSingleThreadScheduledExecutor();
		executor.scheduleAtFixedRate(new Runnable() {
				public void run() {
					MainRoutine();
				}
			}, 100, 100, TimeUnit.MILLISECONDS);

		//thread = new Thread(this);
		//thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO 自動生成されたメソッド・スタブ
		executor.shutdown();
	}

	public void MainRoutine() {
		Canvas canvas = null;

		synchronized(holderMainView) {
			long pastTime;
			long nowTime;

			nowTime = SystemClock.currentThreadTimeMillis();

			try {
				canvas = holderMainView.lockCanvas();
				synchronized (holderMainView) {
					drawAll(canvas);
				}
			} finally {
				if ( canvas != null ) {
					holderMainView.unlockCanvasAndPost(canvas);
				}
			}

			pastTime = SystemClock.currentThreadTimeMillis();

			if ( (pastTime - nowTime) < 100 ) {
				waitTime = 100 - (pastTime - nowTime);
				try {
					Thread.sleep(waitTime);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
}