SENSOR: direction, gravity, magnetic field, temperature, close distance, pressure, light, acceleration
The following shows how to use the direction sensor Compass ):
Use custom view to draw a compass
650) this. width = 650; "style =" width: 250px; height: 313px "title =" qq 30902212529.jpg "border =" 0 "hspace =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1034156062-0.jpg "width =" 250 "height =" 313 "/>
Public class MainActivity extends Activity {private SensorManager manager; private Sensor mSensor; float [] mValues; private SampleView mView; @ Override ("deprecation") @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mView = new SampleView (this); // use custom view setContentView (mView); // get sensor manager = (SensorManager) getSystemService (Context. S ENSOR_SERVICE); // obtain the direction sensor mSensor = manager. getdefasensensor (Sensor. TYPE_ORIENTATION);} SensorEventListener mListener = new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent event) {mValues = event. values; if (mView! = Null) {mView. invalidate (); // re-paint when event changes} @ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {// exact change, applicable to games }}; @ Override protected void onStart () {super. onStart (); // In onstart () or onresume (), the manager registers the listener event manager. registerListener (mListener, mSensor, SensorManager. SENSOR_DELAY_GAME) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. mai N, menu); return true;} private class SampleView extends View {private Paint mPaint = new Paint (); private Path mPath = new Path (); private boolean mAnimate; private long mNextTime; public SampleView (Context context) {super (context); // Construct a wedge-shaped path mPath. moveTo (0,-50); mPath. lineTo (-20, 60); mPath. lineTo (0, 50); mPath. lineTo (20, 60); mPath. close () ;}@ Override protected void OnDraw (Canvas canvas) {Paint paint = mPaint; canvas. drawColor (Color. WHITE); paint. setAntiAlias (true); paint. setColor (Color. BLACK); paint. setStyle (Paint. style. FILL); int w = canvas. getWidth (); int h = canvas. getHeight (); int cx = w/2; int cy = h/2; canvas. translate (cx, cy); if (mValues! = Null) {canvas. rotate (-mValues [0]);} canvas. drawPath (mPath, mPaint) ;}@ Override protected void onAttachedToWindow () {mAnimate = true; super. onAttachedToWindow () ;}@ Override protected void onDetachedFromWindow () {mAnimate = false; super. onDetachedFromWindow ();}}}
This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1287568