欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android利用Senser實(shí)現(xiàn)不同的傳感器

 更新時(shí)間:2022年09月20日 09:45:58   作者:歲月靜好_Cindy  
這篇文章主要為大家詳細(xì)介紹了Android利用Senser實(shí)現(xiàn)不同傳感器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

傳感器有不同的類型,以下是我列出的光線,加速度,風(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();



 }
 //注冊一個(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)動畫
  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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論