Android光線傳感器使用方法詳解
本文實例為大家分享了Android光線傳感器使用的具體代碼,供大家參考,具體內(nèi)容如下
一、首先是布局頁面activity_light_sensor.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? tools:context=".LightSensorActivity"> ? ? <TextView ? ? ? ? android:id="@+id/textView" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="60dp" ? ? ? ? android:gravity="center" ? ? ? ? android:text="光線傳感器" ? ? ? ? android:textColor="@color/black" ? ? ? ? android:textSize="20sp" /> ? ? <EditText ? ? ? ? android:id="@+id/editText" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" /> </LinearLayout>
二、在對應(yīng)的Activity中獲取光線傳感器的值LightSensorActivity,具體注釋已經(jīng)在代碼中給出
public class LightSensorActivity extends AppCompatActivity implements SensorEventListener { ? ? private EditText editText; ? ? //傳感器管理器對象 ? ? private SensorManager sensorManager; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_light_sensor); ? ? ? ? editText = findViewById(R.id.editText); ? ? ? ? sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); ? ? } ? ? @Override ? ? protected void onResume() { ? ? ? ? super.onResume(); ? ? ? ? //第一個參數(shù):SensorEventListener對象用this來指定就可以了 ? ? ? ? // 第二個參數(shù):傳感器對象 光線傳感器類型的常量:TYPE_LIGHT ? ? ? ? // 第三個參數(shù):傳感器數(shù)據(jù)的頻率 這里采用適合游戲的頻率 ? ? ? ? sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME); ? ? } ? ? @Override ? ? protected void onPause() { ? ? ? ? super.onPause(); ? ? ? ? sensorManager.unregisterListener(this); ? ? } ? ? //當(dāng)傳感器的值,發(fā)生變化時,回調(diào)的方法 ? ? @Override ? ? public void onSensorChanged(SensorEvent event) { ? ? ? ? //獲取傳感器的值 ? ? ? ? float[] values= event.values; ? ? ? ? //獲取傳感器類型 ? ? ? ? int sensorType = event.sensor.getType(); ? ? ? ? StringBuilder stringBuilder = null; ? ? ? ? if (sensorType==Sensor.TYPE_LIGHT){ ? ? ? ? ? ? stringBuilder = new StringBuilder(); ? ? ? ? ? ? stringBuilder.append("光的強度值:"); ? ? ? ? ? ? //添加獲取的傳感器的值 ? ? ? ? ? ? stringBuilder.append(values[0]); ? ? ? ? ? ? editText.setText(stringBuilder.toString()); ? ? ? ? } ? ? } ? ? //當(dāng)傳感器的精度,發(fā)生變化時,回調(diào)的方法 ? ? @Override ? ? public void onAccuracyChanged(Sensor sensor, int accuracy) { ? ? } }
效果如圖所示:
以上是光線傳感器的簡單使用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?Activity通用懸浮可拖拽View封裝的思路詳解
這篇文章主要介紹了Android?Activity通用懸浮可拖拽View封裝,實現(xiàn)思路是通過封裝通用的基礎(chǔ)懸浮View,繼承通用View,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07Android GuideView實現(xiàn)首次登陸引導(dǎo)
這篇文章主要為大家詳細介紹了Android GuideView實現(xiàn)首次登陸引導(dǎo),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03Android JSON數(shù)據(jù)與實體類之間的相互轉(zhuǎn)化(GSON的用法)
這篇文章主要介紹了Android JSON數(shù)據(jù)與實體類之間的相互轉(zhuǎn)化(GSON的用法),非常具有實用價值,需要的朋友可以參考下。2017-01-01Android實現(xiàn)動態(tài)向Gallery中添加圖片及倒影與3D效果示例
這篇文章主要介紹了Android實現(xiàn)動態(tài)向Gallery中添加圖片及倒影與3D效果的方法,涉及Android針對圖片的加載、顯示、翻轉(zhuǎn)、倒影等相關(guān)特效功能實現(xiàn)技巧2016-08-08RecyclerView實現(xiàn)流式標(biāo)簽單選多選功能
RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。這篇文章主要介紹了RecyclerView實現(xiàn)的流式標(biāo)簽單選多選功能,需要的朋友可以參考下2019-11-11Android GridView 滑動條設(shè)置一直顯示狀態(tài)(推薦)
這篇文章主要介紹了Android GridView 滑動條設(shè)置一直顯示狀態(tài)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12