Android利用Senser实现不同的传感器
传感器有不同的类型,以下是我列出的光线,加速度,风向传感器,在测试不同传感器的时候都需将传感器管理的onResume中sensorManager.registerListener(myListner,sensorOri,sensorManager.SENSOR_DELAY_UI);
第二个参数改为相应的传感器,此dem中我加入了一张指南针图片作为示例:
activity_main.xml
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看所有支持的传感类型" android:onClick="getAllSensors" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:id="@+id/tv_main_result" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/sounth" android:id="@+id/iv_main_images" />
java代码中注释掉的部分都是一种传感器的测试。
MainActivity.java
package com.example.cindy_sounth; 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.RotateAnimation; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { private SensorManager sensorManager; private Sensor sensorLight; private Sensor sensorAcc; private Sensor sensorOri; private TextView tv_main_result; private MyListner myListner; private ImageView iv_main_images; private float current; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到图片 iv_main_images = (ImageView) findViewById(R.id.iv_main_images); tv_main_result = (TextView) findViewById(R.id.tv_main_result); //得到传感器的管理者 sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); //得到光线传感器 // sensorLight = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //获得加速度传感器 // sensorAcc = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //获取风向传感器 sensorOri = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); myListner = new MyListner(); } //注册一个监听(监听某一个传感器的值) @Override protected void onResume() { super.onResume(); sensorManager.registerListener(myListner,sensorOri,sensorManager.SENSOR_DELAY_UI); } class MyListner implements SensorEventListener{ private WindowManager.LayoutParams layoutParams; //当你的值发生改变 @Override public void onSensorChanged(SensorEvent event) { float[] f=event.values; //测试获取光线传感器的值(光线值) // float light= f[0]; // tv_main_result.setText(light+""); //测试获得加速度传感器 // float x= f[0]; // float y= f[1]; // float z= f[2]; // tv_main_result.setText("x="+x+"\ny="+y+"\nz="+z); //测试获取风向传感器 // float x= f[0]; // float y= f[1]; // float z= f[2]; // tv_main_result.setText("x="+x+"\ny="+y+"\nz="+z); //加图片测试指南针 float x= f[0]; float y= f[1]; float z= f[2]; tv_main_result.setText("x="+x+"\ny="+y+"\nz="+z); //实例化旋转动画 RotateAnimation rotateAnimation=new RotateAnimation(current,-x,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(200); current=-x; iv_main_images.startAnimation(rotateAnimation); //改变屏幕的亮度 //先拿到屏幕 // WindowManager.LayoutParams layoutParams= getWindow().getAttributes(); // layoutParams.screenBrightness=light/225f; // getWindow().setAttributes(layoutParams); } //当值发生精度改变 @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } @Override protected void onDestroy() { super.onDestroy(); sensorManager.unregisterListener(myListner); } public void getAllSensors(View view){ List<Sensor> sensors= sensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor sensor : sensors) { Log.i("test", sensor.getName()); // sensor.getPower(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)