Android利用Sensor實(shí)現(xiàn)傳感器功能
本文實(shí)例為大家分享了Android利用Sensor實(shí)現(xiàn)傳感器的具體代碼,供大家參考,具體內(nèi)容如下
一、傳感器的使用
1、傳感器的類型:
方向傳感器::Sensor.TYPE_ORIENTATION
加速度(重力)傳感器:sensor.TYPE_ACCELEFOMETER
光線傳感器:sensor.TYPT_LIGHT
磁場傳感器:sensor.TYPE_MANGNETIC_FIELD
距離(臨近性)傳感器:Sensor.TYPE_FROXIMITY
溫度傳感器:Sensor.TYPE_TEMPERATURE
常用的API:
<1>得到傳感器的服務(wù)(得到傳感器的管理者)
SensorManager sm=(SensorManager)getSystemService(SENSOR_SERVICE);
<2>得到手機(jī)所支持的所有的傳感器的類型:
List list=sm.getSensorList(SensorManager.TYPE_ALL);
<3>傳感器的類型:
Sensor.getType();
<4>傳感器的名字;
Sensor.getName();
<5>傳感器的監(jiān)聽:SensorEventListener()
sm.registerListener(監(jiān)聽,傳感器對(duì)象,rate);
重點(diǎn):
<1>光線傳感器:sensor.TYPT_LIGHT
得到光線值:float f=event.values[0];
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
params.screenBrightness = value / 255f;
activity.getWindow().setAttributes(params);
<2>加速度傳感器:sensor.TYPE_ACCELEFOMETER
加速度有三個(gè)值:這三個(gè)值是手機(jī)在三個(gè)方向受到的加速度
float x=event.values[0];–>在手機(jī)頂部從左邊沿往有邊沿是手機(jī)的X軸的正方向
float y=event.values[1];–>從手機(jī)頂部沿手機(jī)左邊沿手機(jī)底部是Y軸的正方向
float z=event.values[2];–>垂直手機(jī)屏幕朝外的是正方向
<3>方向傳感器:Sensor.TYPE_ORIENTATION
方向傳感器三個(gè)值:
方向角:指手機(jī)平躺時(shí),手機(jī)頭部繞Z軸旋轉(zhuǎn),與地球正北極的夾角
0代表北(North)
90代表東East
180代表南(South)
270代表西(West)
俯視角:手機(jī)繞X軸旋轉(zhuǎn)與水平線的夾角
滾轉(zhuǎn)角:手機(jī)繞Y軸旋轉(zhuǎn)與水平線的夾角
利用方向傳感器實(shí)現(xiàn) 指南針應(yīng)用
運(yùn)行后效果圖如下:


布局文件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.g150825_android29.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/znz"
android:id="@+id/iv_image"
/>I
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/tv_main_result"
/>
</RelativeLayout>
Java代碼(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);
//得到傳感器管理者
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//得到光線傳感器
// sensorLight = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
//獲取加速度傳感器
// sensorACC = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//獲取方向傳感器
sensorOri=sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
//獲取光線傳感器的值(光線值)
myListener = new MyListener();
}
//注冊(cè)監(jiān)聽(監(jiān)聽某一個(gè)傳感器的值)
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(myListener,sensorOri,SensorManager.SENSOR_DELAY_UI);
}
class MyListener implements SensorEventListener{
//當(dāng)值發(fā)生改變
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float[] f=sensorEvent.values;
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, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(200);
current=-x;
iv_image.startAnimation(rotateAnimation);
//改變屏幕的亮度
// WindowManager.LayoutParams layoutParams=getWindow().getAttributes();
// layoutParams.screenBrightness=light/255f;
// getWindow().setAttributes(layoutParams);
}
//當(dāng)值的精度發(fā)生改變
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
//取消注冊(cè)監(jiān)聽
@Override
protected void onDestroy() {
super.onDestroy();
sensorManager.unregisterListener(myListener);
}
// public void getAllSensors(View view){
// List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
// for(Sensor s:sensors){
// Log.i("test", s.getName());
// }
//
// }
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例
對(duì)于不同的屏幕密度、不同的設(shè)備方向,不同的語言和區(qū)域,都會(huì)涉及到備選 drawable 資源,下面這篇文章主要給你大家介紹了關(guān)于Android自定義Drawable之在Drawable中部指定透明區(qū)域的相關(guān)資料,需要的朋友可以參考下2018-07-07
Android實(shí)現(xiàn)中國象棋游戲(局域網(wǎng)版)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)局域網(wǎng)版的中國象棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
超簡單Android集成華為HMS Scankit 掃碼SDK實(shí)現(xiàn)掃一掃二維碼
這篇文章主要介紹了超簡單Android集成華為HMS Scankit 掃碼SDK實(shí)現(xiàn)掃一掃二維碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android中fragment與activity之間的交互(兩種實(shí)現(xiàn)方式)
本篇文章主要介紹了Android中fragment與activity之間的交互(兩種實(shí)現(xiàn)方式),相信對(duì)大家學(xué)習(xí)會(huì)有很好的幫助,需要的朋友一起來看下吧2016-12-12
Android提高之模擬信號(hào)示波器的實(shí)現(xiàn)
這篇文章主要介紹了Android模擬信號(hào)示波器的實(shí)現(xiàn)方法,在Android項(xiàng)目開發(fā)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
AndroidStudio 設(shè)置格式化斷行寬度教程
這篇文章主要介紹了AndroidStudio 設(shè)置格式化斷行寬度教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法
本篇文章主要介紹了詳解Android Activity之間跳轉(zhuǎn)出現(xiàn)短暫黑屏的處理方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06

