Android gravity sensor application [Android evolution 17th]

Source: Internet
Author: User

 

Gravity sensing is mainly used in game development. This effect is very good! It is mainly three axes. It is easy to understand: that is facing up, the value is positive, and the value is down negative! I have tested this effect, because gravity sensing can be realized on the mobile phone, so no. Sorry! Reprinted please indicate the source: http://blog.csdn.net/wdaming1986/article/details/6752232

 

1. MainActivity. Java code:

 

Package com. ray. test;

 

Import android. app. Activity;

Import android. hardware. Sensor;

Import android. hardware. SensorEvent;

Import android. hardware. SensorEventListener;

Import android. hardware. SensorManager;

Import android. OS. Bundle;

Import android. widget. TextView;

 

Public class MainActivity extends Activity {

 

Private SensorManager sensorMgr;

Private TextView show_TextView;

Sensor sensor;

Private float x, y, z;

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

Show_TextView = (TextView) findViewById (R. id. text_view );

SensorMgr = (SensorManager) getSystemService (SENSOR_SERVICE );

Sensor = sensorMgr. getdefasensensor (Sensor. TYPE_ACCELEROMETER );

SensorEventListener lsn = new SensorEventListener ()

{

@ Override

Public void onSensorChanged (SensorEvent e ){

// TODO Auto-generated method stub

X = e. values [SensorManager. DATA_X];

Y = e. values [SensorManager. DATA_Y];

Z = e. values [SensorManager. DATA_Z];

SetTitle ("x =" + (int) x + "," + "y =" + (int) y + "," + "z =" + (int) z );

Show_TextView.setText ("x =" + (int) x + "," + "y =" + (int) y + "," + "z =" + (int) z );

}

 

@ Override

Public void onAccuracyChanged (Sensor arg0, int arg1 ){

}

}; // Register the listener. The third parameter is the detection accuracy.

SensorMgr. registerListener (lsn, sensor, SensorManager. SENSOR_DELAY_GAME );

}

 

}

Ii. main. xml code: www.2cto.com

 

<? Xml version = "1.0" encoding = "UTF-8"?>

 

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

<TextView

Android: id = "@ + id/text_view"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: textSize = "25pt"

/>

 

</LinearLayout>

Related Article

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.