最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

傳感器有不同的類型,以下是我列出的光線,加速度,風(fēng)向傳感器,在測(cè)試不同傳感器的時(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代碼中注釋掉的部分都是一種傳感器的測(cè)試。

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;
  //測(cè)試獲取光線傳感器的值(光線值)
//  float light= f[0];
//  tv_main_result.setText(light+"");

  //測(cè)試獲得加速度傳感器
//  float x= f[0];
//  float y= f[1];
//  float z= f[2];
//  tv_main_result.setText("x="+x+"\ny="+y+"\nz="+z);

  //測(cè)試獲取風(fēng)向傳感器
//  float x= f[0];
//  float y= f[1];
//  float z= f[2];
//  tv_main_result.setText("x="+x+"\ny="+y+"\nz="+z);

  //加圖片測(cè)試指南針
  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();
 }

 }



}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android?側(cè)滑按鈕的實(shí)現(xià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)營(yíng)商代碼實(shí)例

    android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營(yíng)商代碼實(shí)例

    這篇文章主要介紹了android獲取手機(jī)IMSI碼判斷手機(jī)運(yùn)營(yíng)商代碼實(shí)例,大家參考使用
    2013-11-11
  • Android 在其他線程中更新UI線程的解決方法

    Android 在其他線程中更新UI線程的解決方法

    本篇文章是對(duì)Android中在其他線程中更新UI線程的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android利用BitMap獲得圖片像素?cái)?shù)據(jù)的方法

    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按鈕是灰色的快速解決方法

    這篇文章主要介紹了Android Studio中Run按鈕是灰色的快速解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-03-03
  • Android 通用型手電筒代碼

    Android 通用型手電筒代碼

    說(shuō)到手機(jī)手電筒功能,很多人都是直接調(diào)用閃光燈,而本文給大家介紹的是用相機(jī)功能來(lái)實(shí)現(xiàn)的,有需要的小伙伴可以參考下。
    2015-06-06
  • Android實(shí)現(xiàn)懸浮圖片

    Android實(shí)現(xiàn)懸浮圖片

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)懸浮圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Android AndFix熱修復(fù)原理詳情

    Android AndFix熱修復(fù)原理詳情

    這篇文章主要介紹了Android AndFix熱修復(fù)原理詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值嗎,需要的小伙伴可以參考一下
    2022-08-08
  • Android中的存儲(chǔ)詳解

    Android中的存儲(chǔ)詳解

    大家好,本篇文章主要講的是Android中的存儲(chǔ)詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Android使用Handler和Message更新UI

    Android使用Handler和Message更新UI

    這篇文章主要介紹了Android使用Handler和Message更新UI的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評(píng)論

温州市| 衡阳县| 珠海市| 鸡东县| 中牟县| 江达县| 两当县| 乌拉特前旗| 方山县| 长泰县| 望奎县| 阿巴嘎旗| 桐乡市| 建昌县| 东城区| 宜兰县| 贡山| 汪清县| 读书| 晋中市| 边坝县| 文登市| 舞钢市| 黄浦区| 法库县| 丹巴县| 乐陵市| 三门县| 咸阳市| 莲花县| 奇台县| 尼木县| 兰溪市| 三原县| 新化县| 克拉玛依市| 漳州市| 阿克陶县| 宁武县| 临桂县| 碌曲县|