How to Use the direction Sensor in android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.