Android利用Senser實(shí)現(xiàn)不同的傳感器
傳感器有不同的類型,以下是我列出的光線,加速度,風(fēng)向傳感器,在測試不同傳感器的時(shí)候都需將傳感器管理的onResume中sensorManager.registerListener(myListner,sensorOri,sensorManager.SENSOR_DELAY_UI);
第二個(gè)參數(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();
}
//注冊(cè)一個(gè)監(jiān)聽(監(jiān)聽某一個(gè)傳感器的值)
@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);
//實(shí)例化旋轉(zhuǎn)動(dòng)畫
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 利用方向傳感器實(shí)現(xiàn)指南針具體步驟
- Android利用方向傳感器獲得手機(jī)的相對(duì)角度實(shí)例說明
- Android編程中光線傳感器的調(diào)用方法詳解
- Android編程使用加速度傳感器實(shí)現(xiàn)搖一搖功能及優(yōu)化的方法詳解
- Android編程實(shí)現(xiàn)獲取所有傳感器數(shù)據(jù)的方法
- Android利用Sensor(傳感器)實(shí)現(xiàn)水平儀功能
- Android 傳感器--光照傳感器詳解及使用
- Android 重力傳感器在游戲開發(fā)中的應(yīng)用
- Android重力傳感器實(shí)現(xiàn)滾動(dòng)的彈球
- android 傳感器(OnSensorChanged)使用介紹
- Android編程之方向傳感器用法示例
相關(guān)文章
Android?側(cè)滑按鈕的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android?側(cè)滑按鈕的實(shí)現(xiàn),本文結(jié)合示例代碼圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營商代碼實(shí)例
這篇文章主要介紹了android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營商代碼實(shí)例,大家參考使用2013-11-11
Android利用BitMap獲得圖片像素?cái)?shù)據(jù)的方法
這篇文章主要介紹了Android利用BitMap獲得圖片像素?cái)?shù)據(jù)的方法,結(jié)合實(shí)例對(duì)比分析了Android獲取圖片像素?cái)?shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-02-02
Android Studio中Run按鈕是灰色的快速解決方法
這篇文章主要介紹了Android Studio中Run按鈕是灰色的快速解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03

