Android開発は初めてです。ユーザーが画面に触れるたびにビットマップ画像を描画しようとしています。画面上で発生するタッチ イベントの x と y の位置を取得できました。しかし、以下のプログラムがキャンバスに画像を描画しない理由がわかりません。
ここに私が取り組んでいるコードがあります:
public class SurfaceViewTest extends Activity implements OnTouchListener {
FastRenderView renderView;
Bitmap bob565;
Bitmap bob4444;
float x,y;
Canvas canvas = new Canvas();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
renderView = new FastRenderView(this);
setContentView(renderView);
}
protected void onResume() {
super.onResume();
renderView.resume();
}
protected void onPause() {
super.onPause();
renderView.pause();
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY()
canvas.drawBitmap(bob4444, x, y, null);
break;
}
return true;
}
class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;
public FastRenderView(Context context) {
super(context);
holder = getHolder();
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("bobrgb888.png");
bob565 = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("BitmapText",
"bobrgb888.png format: " + bob565.getConfig());
inputStream = assetManager.open("bobargb8888.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bob4444 = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
Log.d("BitmapText",
"bobargb8888.png format: " + bob4444.getConfig());
} catch (IOException e) {
} finally {
// we should really close our input streams here.
}
}
public void resume() {
running = true;
renderThread = new Thread(this);
renderThread.start();
}
public void run() {
while(running) {
if(!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
canvas.drawRGB(0, 0, 255);
canvas.drawBitmap(bob4444, x, y, null);
holder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
running = false;
while(true) {
try {
renderThread.join();
return;
} catch (InterruptedException e) {
// retry
}
}
}
}
}