본문 바로가기

정보기술, IT/IT source

sensor를 이용한 도형움직이기.

▶▶ Sensor를 이용해 도형을 움직여보자

 


카메라 기울기에 따라 도형이 움직이는 모습.

가속도 센서를 사용하였다.

TYPE_ACCELEROMETER


 public class MyView extends View {

	private Drawable image;

	private int viewWidth;
	private int viewHeight;
	private int imageWidth;
	private int imageHeight;
	private float x;
	private float y;

	public MyView(Context context, AttributeSet attrs) { //이미지 불러오기
		super(context, attrs);
		image = this.getResources().getDrawable(R.drawable.one);
	}
	
	public void move(float mx, float my) { //화면의 가
		this.x -= (mx * 4f);
		this.y += (my * 4f);
		Log.i("viewHeight =", viewHeight + ""); 
		if (this.x < 0) {
			this.x = 0;
		} else if ((this.x + imageWidth) > this.viewWidth) {
			this.x = this.viewWidth - imageWidth;
		}

		if (this.y < 0) {
			this.y = 0;
		} else if ((this.y + imageHeight) > this.viewHeight) {
			this.y = this.viewHeight - imageHeight;
		}

		Log.i("SENSOR", "movie-[this.x]" + x + "[this.y]" + y + "[mx]" + mx
				+ "[my]" + my);
		this.invalidate();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
//화면을 보여줄 view의 너비를 구해줌.
		this.viewWidth = this.getWidth();
		this.viewHeight = this.getHeight();

		imageWidth = image.getIntrinsicWidth();
		imageHeight = image.getIntrinsicHeight();

		super.onSizeChanged(w, h, oldw, oldh); 
//부모클래스에 있는 메서드를 불러 줌.
	}

	@Override
	protected void onDraw(Canvas canvas) {
//이미지를 그려줌.

int xx = (int) (this.x); int yy = (int) (this.y); image.setBounds(xx, yy, xx + imageWidth, yy + imageHeight); image.draw(canvas); super.onDraw(canvas); } };

 public class MainActivity extends Activity {

private SensorManager sm; private Display mDisplay; // 디스플레이 크기의 정보값을 받아옵니다. private WindowManager mWin; // 디스플레이의 context를 얻어온다. (방향 전환) private SensorEventListener accL; // 가속도 private SensorEventListener gyroL; // 회전 private Sensor accSensor; // 가속도 private Sensor gyroSensor; // 회전 private MyView image; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); mWin = (WindowManager) getSystemService(WINDOW_SERVICE); mDisplay = mWin.getDefaultDisplay(); image = new MyView(this, null); setContentView(image); sm = (SensorManager) getSystemService(SENSOR_SERVICE); accSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); accL = new Listener(); // 가속도 센서 리스너 인스턴스 } @Override protected void onResume() { super.onResume(); sm.registerListener(accL, accSensor, SensorManager.SENSOR_DELAY_FASTEST);// 가속도 } @Override protected void onPause() { super.onPause(); if (sm != null) { sm.unregisterListener(accL); } } @Override protected void onDestroy() { super.onDestroy(); if (sm != null) { sm.unregisterListener(accL); } } private class Listener implements SensorEventListener { @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { ; } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return; switch (mDisplay.getRotation()) { case Surface.ROTATION_0: image.move(event.values[SensorManager.DATA_X], event.values[SensorManager.AXIS_Y]); break; case Surface.ROTATION_90: image.move(-event.values[SensorManager.AXIS_X], event.values[SensorManager.AXIS_Y]); break; case Surface.ROTATION_270: image.move(event.values[SensorManager.AXIS_X], -event.values[SensorManager.AXIS_Y]); break; } } } };