package com.tistory.jwandroid.surfaceview;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewTest01Activity extends Activity {
Bitmap testImage01;
MyViewThread mThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MySurfaceView(this));
testImage01 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{
public MySurfaceView(Context context) {
super(context);
getHolder().addCallback( this );
}
public void surfaceCreated(SurfaceHolder holder) {
mThread = new MyViewThread(getHolder()); // GameThread 생성
mThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean done = true;
while (done) {
try {
mThread.join();
done = false;
}catch (InterruptedException e) {
}
}
}
}
class MyViewThread extends Thread{
private SurfaceHolder surfaceholder;
private boolean running = true;
int imgX = 0;
int imgY = 0;
MyViewThread(SurfaceHolder _holder){
surfaceholder = _holder;
}
public void run(){
Canvas c;
while (running) {
c = null;
try {
c = surfaceholder.lockCanvas(null);
synchronized (surfaceholder) {
drawBackground(c);
drawMyBitmap(c);
}
} finally {
if (c != null) {
surfaceholder.unlockCanvasAndPost(c);
}
}
}
}
public void drawBackground(Canvas _canvas){
_canvas.drawARGB(255, 255, 255, 255);
}
private void drawMyBitmap(Canvas _canvas){
_canvas.drawBitmap(testImage01, imgX, imgY, null);
imgX++;
imgY++;
}
}
}