package com.test.acc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class HardAcceleratorActivity extends Activity {

	SensorManager mSm;

	AcceleratorView mView;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		mView = new AcceleratorView(this);

		setContentView(mView);

		mSm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

	}

	protected void onResume() {

		super.onResume();

		mSm.registerListener(mView, mSm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
		mSm.registerListener(mView, mSm.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_FASTEST);

	}

	protected void onPause() {

		super.onPause();

		mSm.unregisterListener(mView);

	}

	class AccelValue {

		float x;

		float y;

		float z;

	}

	class AcceleratorView extends View implements SensorEventListener {

		int width;

		int height;

		int w10;

		ArrayList <AccelValue> arValue = new ArrayList<AccelValue>();

		final static int MAGX = 2;

		final static int MAGY = 1;

		boolean mStop = false;

		public AcceleratorView(Context context) {

			super(context);

		}

		public void onSizeChanged(int w, int h, int oldw, int oldh) {

			super.onSizeChanged(w, h, oldw, oldh);

			width = w;

			height = h;

			w10 = width / 10;

		}

		public void onAccuracyChanged(Sensor sensor, int accuracy) {

		}

		float G=0;
		float[] R=new float[9];
		public void onSensorChanged(SensorEvent event) {

			if (mStop) {

				return;

			}

			float[] v = event.values;
			switch (event.sensor.getType()) {
			case Sensor.TYPE_ROTATION_VECTOR:
				SensorManager.getRotationMatrixFromVector(R, event.values);
				break;
			case Sensor.TYPE_LINEAR_ACCELERATION:
			case Sensor.TYPE_GRAVITY:
			case Sensor.TYPE_ACCELEROMETER:
				if (arValue.size() * MAGY > height) {

					arValue.clear();

				}

				if(G==0){
					G=v[2];
				}
				AccelValue av = new AccelValue();

				av.x = v[0] * R[ 3] + v[1] * R[1] + v[2] * R[2];

				av.y = v[0] * R[3] + v[1] * R[4] + v[2] * R[5];

				av.z = v[0] * R[6] + v[1] * R[7] + v[2] * R[8];
				// 이 위의 세줄이 가장 중요 

				arValue.add(av);

				invalidate();

				break;

			}

		}

		float sumX = 0;
		float sumY = 0;
		float sumZ = 0;
		public void onDraw(Canvas canvas) {

			Paint Pnt = new Paint();

			Pnt.setColor(Color.WHITE);

			Pnt.setTextSize(20);

			Paint linePaint = new Paint();

			linePaint.setColor(0xff404040);

			int i;

			int x, y;

			int oldx, oldy;

			int basex;

			// X 가속 그림

			basex = oldx = w10 * 2;

			oldy = 0;

			canvas.drawText("X", basex - 25, 25, Pnt);

			canvas.drawLine(basex, 0, basex, height, linePaint);

			for (i = 0; i < arValue.size(); i++) {

				x = (int) (basex + arValue.get(i).x * MAGX);

				y = i * MAGY;
				canvas.drawLine(oldx, oldy, x, y, Pnt);
				oldx = x;
				oldy = y;

			}

			// Y 가속 그림

			basex = oldx = w10 * 5;

			oldy = 0;

			canvas.drawText("Y", basex - 25, 25, Pnt);

			canvas.drawLine(basex, 0, basex, height, linePaint);

			for (i = 0; i < arValue.size(); i++) {

				x = (int) (basex + arValue.get(i).y * MAGX);

				y = i * MAGY;

				canvas.drawLine(oldx, oldy, x, y, Pnt);

				oldx = x;

				oldy = y;

			}

			// Z 가속 그림

			basex = oldx = w10 * 8;

			oldy = 0;

			canvas.drawText("Z", basex - 25, 25, Pnt);

			canvas.drawLine(basex, 0, basex, height, linePaint);

			for (i = 0; i < arValue.size(); i++) {

				x = (int) (basex + (arValue.get(i).z -G) * MAGX);
				
				y = i * MAGY;

				canvas.drawLine(oldx, oldy, x, y, Pnt);

				oldx = x;

				oldy = y;

			}

		}

		public boolean onTouchEvent(MotionEvent event) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {

				mStop = !mStop;

				return true;

			}

			return false;

		}

	}

}

참조 : http://www.winapi.co.kr/android/annex/19-2.htm

알고보니 winapi 주인이 안드로이드 완전정복 저자였네
사이트는 http://www.winapi.co.kr/android/ 여기

참고로 레이아웃 이딴거 필요없고 소스코드 복붙애서 실행하면 끝
Posted by 동적할당
: