안녕하세요 많이 초보인 KD라고 합니다
현재 안드로이드 시작한지 한달째인 새내기 인데
지금은 캔버스에 그림 올리고 서페이스 뷰써서 배경화면이 스크롤 되구요
캐릭터가 디바이스 왼쪽으로 하면 왼쪽으로 가고 오른쪽으로 가면 오른쪽으로 가야되는데
센서 사용법도 잘 모르겠고...
막상 소스를 쓰긴 썻는데 스레드 런에 넣어야 할지 구문을 어디에 넣어야 할지 정말 모르겠네요... 후...
sm = (SensorManager)context.getSystemService(context.SENSOR_SERVICE);
accSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
acc = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
float var0 = event.values[0]; //x 값의 센서데이타를 가져옴
float var1 = event.values[1]; //y 값의 센서데이타를 가져옴
float var2 = event.values[2]; //z 값의 센서데이타를 가져옴
y3 = Height; // 이미지를 아래로 다시 이동
}
public void onAccuracyChanged(Sensor sensor , int accuracy) {
}
};




전체 소스입니다...
import java.util.Random;
import kr.co.example.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;
public class BalloonView extends SurfaceView implements Callback {
private GameThread mThread; // 스레드
private SurfaceHolder mSurfaceHolder; // SurfaceHolder
private int Width, Height, cx, cy; // 화면의 전체 폭과 중심점
private int x1, y1, x2, y2; // Viewport 좌표
private int x3, y3; // 바위의 위치
private int sx1, sy1; // Viewport가 1회에 이동할 거리
private Bitmap imgBack1; // 배경 이미지
private Bitmap balloon , rock; // 풍선 이미지
private int w, h , w2; // 풍선의 폭과 높이
private int w3, h3; // 바위의 폭과 높이
private long counter = 0; // 루프의 전체 반복 횟수
private int minus = -1;
private boolean canRun = true; // 스레드 실행용 플래그
private Random rnd = new Random();
private int ballW , ballH , ballX , ballY , rockW , rockH , rockX , rockY ;
private int balloonSu = 2;
private Resources res;
private SensorManager sm;
private SensorEventListener acc;
private Sensor accSensor;
//--------------------------------------
// 생 성 자
//--------------------------------------
public BalloonView(Context context) {
super(context);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
// 화면 해상도 구하기
Display display = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE)).getDefaultDisplay();
Width = display.getWidth();
Height = display.getHeight();
cx = Width / 2; // 화면의 중심점
cy = Height / 5;
Resources res = context.getResources(); // 리소스 읽기
// 배경화면 읽고 배경화면의 크기를 화면의 크기로 조정
imgBack1 = BitmapFactory.decodeResource(res, R.drawable.backimg);
rock = BitmapFactory.decodeResource(res, R.drawable.rock);
imgBack1 = Bitmap.createScaledBitmap(imgBack1, Width, Height, true);
// 우주선 읽고 폭과 높이 계산
balloon = BitmapFactory.decodeResource(res, R.drawable.balloon3);
// balloon = BitmapFactory.decodeResource(res, R.drawable.balloon2);
// balloon = BitmapFactory.decodeResource(res, R.drawable.balloon1);
w = balloon.getWidth() / 2;
h = balloon.getHeight() / 2;
w3 = rock.getWidth();
h3 = rock.getHeight() / 2;
w2 = balloon.getWidth();
x1 = 0; // Viewport의 시작 위치는 이미지의 한가운데
y1 = 0;
sx1 = -2; // Viewport를 1회에 이동시킬 거리
sy1 = 5;
x3 = rnd.nextInt(Width);
y3 = Height;
ballW = balloon.getWidth();
ballH = balloon.getHeight();
ballX = balloon.getWidth() / 2;
ballY = balloon.getHeight() / 2;
rockW = rock.getWidth();
rockH = rock.getHeight();
rockX = rock.getWidth() / 2;
rockY = rock.getHeight() / 2;
mThread = new GameThread(context, holder);
sm = (SensorManager)context.getSystemService(context.SENSOR_SERVICE);
accSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
acc = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
float var0 = event.values[0]; //x 값의 센서데이타를 가져옴
float var1 = event.values[1]; //y 값의 센서데이타를 가져옴
float var2 = event.values[2]; //z 값의 센서데이타를 가져옴
y3 = Height;
}
public void onAccuracyChanged(Sensor sensor , int accuracy) {
}
};
} // 생성자 끝
//--------------------------------------
// Surface가 생성될 때 호출됨
//--------------------------------------
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mThread.start();
}
//--------------------------------------
// Surface가 바뀔 때 호출됨
//--------------------------------------
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
//--------------------------------------
// Surface가 삭제될 때 호출됨
//--------------------------------------
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean done = true;
while (done) {
try {
mThread.join(); // 스레드가 현재 step을 끝낼 때 까지 대기
done = false;
} catch (InterruptedException e) { // 인터럽트 신호가 오면?
// 그 신호 무시 - 아무것도 않음
}
} // while
} // surfaceDestroyed
//--------------- 여기서부터 스레드 영역이야. 넘보지 마! -----------------------
class GameThread extends Thread {
SurfaceHolder mHolder;
//--------------------------------------
// Thread Constructor
//--------------------------------------
public GameThread(Context context, SurfaceHolder holder) {
mHolder = holder;
}
//--------------------------------------
// Thread run
//--------------------------------------
public void run() {
Rect src = new Rect(); // Viewport의 좌표
Rect dst = new Rect(); // View(화면)의 좌표
dst.set(0, 0, Width, Height); // View는 화면 전체 크기
while (canRun) {
Canvas canvas = null;
canvas = mHolder.lockCanvas();
try {
synchronized (mHolder) {
ScrollImage(); // Viewport 이동
src.set(x1, y1, x1 + cx, y1 + cy); // 이동한 Viewport 좌표
canvas.drawBitmap(imgBack1, src, dst, null);
canvas.drawBitmap(balloon, cx - w, cy - h, null);
canvas.drawBitmap(rock, x3, y3, null);
}
} finally {
mHolder.unlockCanvasAndPost(canvas);
} // try
} // while
} // run 끝
//--------------------------------------
// 배경 Scroll
//--------------------------------------
private void ScrollImage() {
counter++;
if (counter % 2 == 0) { // 루프의 2회에 1번씩 스크롤
// x1 += sx1; // Viewport를 위로 이동 (sx는 음수(-)임)
y1 += sy1;
y3 = y3-14;
if (x1 < 0) x1 = cx; // 이미지를 벗어나면 이미지의 중심으로 이동
if (y1 >= Height) y1 = 0;
if (y3 <0) {
x3 = rnd.nextInt(Width)-w3;
y3 = Height;
if(x3 <0) x3 = x3 * -1;
}
if ( Math.abs( (cx-w) - x3 ) < ballW && Math.abs( (cy-h) - y3 ) < ballH ) {
// 충돌에 따른 처리
x3 = rnd.nextInt(Width)-w3;
y3 = Height;
if(x3 <0) x3 = x3 * -1;
//balloonSu = balloonSu-1;
}
}
} // Scroll 끝
} // Thread 끝
// ---------------------------------
// onTouchEvent
// ---------------------------------
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX(); // 클릭한 위치를 Rect()로 만듦
if (x > cx && cx-w < Width -w2) {
w = w -10;
}
else if(x < cx && cx-w > 0 ) {
w = w +10;
}
}
return true;
} // touch
} // SurfaceView 끝