傳感器有不同的類型,以下是我列出的光線,加速度,風(fēng)向傳感器,在測試不同傳感器的時候都需將傳感器管理的onResume中sensorManager.registerListener(myListner,sensorOri,sensorManager.SENSOR_DELAY_UI);
第二個參數(shù)改為相應(yīng)的傳感器,此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); //獲取風(fēng)向傳感器 sensorOri = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); myListner = new MyListner(); } //注冊一個監(jiān)聽(監(jiān)聽某一個傳感器的值) @Override protected void onResume() { super.onResume(); sensorManager.registerListener(myListner,sensorOri,sensorManager.SENSOR_DELAY_UI); } class MyListner implements SensorEventListener{ private WindowManager.LayoutParams layoutParams; //當(dāng)你的值發(fā)生改變 @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); //測試獲取風(fēng)向傳感器// 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); //實例化旋轉(zhuǎn)動畫 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); } //當(dāng)值發(fā)生精度改變 @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(); } }}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答
圖片精選