In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Android uses Sensor to achieve sensor functions. I hope you will get something after reading this article. Let's discuss it together.
The details are as follows
I. the use of sensors
1. Type of sensor:
Direction sensor:: Sensor.TYPE_ORIENTATION
Acceleration (gravity) sensor: sensor.TYPE_ACCELEFOMETER
Light sensor: sensor.TYPT_LIGHT
Magnetic field sensor: sensor.TYPE_MANGNETIC_FIELD
Distance (proximity) sensor: Sensor.TYPE_FROXIMITY
Temperature sensor: Sensor.TYPE_TEMPERATURE
Commonly used API:
Get the service of the sensor (the manager who gets the sensor)
SensorManager sm= (SensorManager) getSystemService (SENSOR_SERVICE)
Get all the types of sensors supported by the phone:
List list=sm.getSensorList (SensorManager.TYPE_ALL)
Type of sensor:
Sensor.getType ()
The name of the sensor
Sensor.getName ()
Sensor monitoring: SensorEventListener ()
Sm.registerListener (monitoring, sensor object, rate)
Key points:
Light sensor: sensor.TYPT_LIGHT
Get the ray value: float f=event.values [0]
WindowManager.LayoutParams params = activity.getWindow () .getAttributes ()
Params.screenBrightness = value / 255f
Activity.getWindow () setAttributes (params)
Acceleration sensor: sensor.TYPE_ACCELEFOMETER
Acceleration has three values: these three values are the acceleration of the phone in three directions
Float x=event.values [0];-> at the top of the phone, from the left edge to the edge is the positive direction of the phone's X axis.
Float y=event.values [1];-> from the top of the phone along the left side of the phone along the bottom of the phone is the positive direction of the Y axis
Float z=event.values [2];-> the vertical phone screen is facing outward in a positive direction
Direction sensor: Sensor.TYPE_ORIENTATION
Three values of the direction sensor:
Bearing: when the phone is lying flat, the head of the phone rotates around the Z axis, including the angle with the positive north pole of the earth.
0 stands for North (North)
90 stands for East East
180 stands for South
270stands for West
Overlooking angle: the angle between the rotation of the mobile phone around the X axis and the horizontal line
Roll angle: the angle between the rotation of the phone around the Y axis and the horizontal line
Using Direction Sensor to realize the Application of Compass
The results after running are as follows:
Layout file (activity_main.xml)
I
Java Code (MainActivity)
Package com.example.g150825_android29;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.WindowManager;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.ImageView;import android.widget.TextView;import java.util.List Public class MainActivity extends AppCompatActivity {private SensorManager sensorManager; private Sensor sensorOri; private TextView tv_main_result; private MyListener myListener; private ImageView iv_image; private float current=0; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); tv_main_result = (TextView) findViewById (R.id.tv_main_result) Iv_image = (ImageView) findViewById (R.id.iv_image); / / get the sensor manager sensorManager = (SensorManager) getSystemService (Context.SENSOR_SERVICE); / / get the light sensor / / sensorLight = sensorManager.getDefaultSensor (Sensor.TYPE_LIGHT); / / get the accelerometer / / sensorACC = sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER) / / get the direction sensor sensorOri=sensorManager.getDefaultSensor (Sensor.TYPE_ORIENTATION); / / get the light sensor value (ray value) myListener = new MyListener ();} / / register monitoring (listening for a sensor value) @ Override protected void onResume () {super.onResume (); sensorManager.registerListener (myListener,sensorOri,SensorManager.SENSOR_DELAY_UI) } class MyListener implements SensorEventListener {/ / when the value is changed @ Override public void onSensorChanged (SensorEvent sensorEvent) {float [] fancisensorEvent.values; float xfolf [0]; float yellowf [1]; float zefff [2]; tv_main_result.setText ("x =" + x + "\ ny=" + y + "\ nz=" + z) / instantiate the rotation animation RotateAnimation rotateAnimation=new RotateAnimation (current,-x, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration (200); current=-x; iv_image.startAnimation (rotateAnimation); / / change the brightness of the screen / / WindowManager.LayoutParams layoutParams=getWindow () .getAttributes () / / layoutParams.screenBrightness=light/255f;// getWindow () .setAttributes (layoutParams);} / when the precision of the value changes @ Override public void onAccuracyChanged (Sensor sensor, int I) {}} / / Unregister the listener @ Override protected void onDestroy () {super.onDestroy (); sensorManager.unregisterListener (myListener) } / / public void getAllSensors (View view) {/ / List sensors=sensorManager.getSensorList (Sensor.TYPE_ALL); / / for (Sensors: sensors) {/ / Log.i ("test", s.getName ()) / /} /}} after reading this article, I believe you have a certain understanding of "how Android uses Sensor to achieve sensor functions". If you want to know more about it, please follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.