国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android利用傳感器仿微信搖一搖功能

2019-12-12 02:57:07
字體:
來源:轉載
供稿:網友

傳感器

簡單的介紹一下傳感器:
就是設備用來感知周邊環境變化的硬件。

Android中的傳感器包含在傳感器框架中,屬于android.hardware.*(硬件部分)

傳感器框架主要包含四個部分:

① SensorManager:用來獲取傳感器的入口,它是一個系統的服務,還可以為傳感器注冊與取消注冊監聽
② Sensor: 具體的傳感器,包含了傳感器的名字,類型,采樣率
③ SensorEvent:傳感器事件,包含了傳感器采集回來的數據,傳感器的精度
④ SensorEventListener:傳感器的監聽,主要監測傳感器數據變化,精度變化…

Android播放音頻系統提供了兩種方式

① MediaPlayer 播放常規的音頻,視頻,通常用在播放器上
② SoundPool 聲音池,通常用在小而頻繁播放的音樂,需要同時播放多個音樂的

VIBRATE 所震動傳感器需要添加權限

<uses-permission android:name="android.permission.VIBRATE"/>

實現之前先來看看手機上的傳感器有哪些?(此處可以略過…)
onCreat();中設置TextView

sensor = (TextView) findViewById(R.id.sensor);    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);    List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);    StringBuilder builder = new StringBuilder();    builder.append("傳感器總數: "+sensorList.size()+"/n");    for (int i = 0; i < sensorList.size(); i++) {      Sensor sensor = sensorList.get(i);      builder.append("傳感器名稱: "+sensor.getName()+", 傳感器生產廠商: "+sensor.getVendor()+"/n");    }    sensor.setText(builder.toString());

驚奇的發現小米5上竟然有41個傳感器,可以哈…厲害了
好了,好了接下來言歸正傳―微信搖一搖

界面的話三張圖片你的層疊,RelativeLayout進行布局

在onCreate中封裝的方法

//搖一搖---->加速傳感器    //1、初始化控件    initView();    //2、初始化音樂SoundPool    initSoundPool();    //3、震動    initVibrator();

上面蓋的兩張圖片初始化

up_logo = (ImageView) findViewById(R.id.up_logo);down_logo = (ImageView) findViewById(R.id.down_logo);

在初始化SoundPool的時候,發現new SoundPool已經不推薦使用了,新版的(API>21)使用Builder構建,所以在這里使用版本進行判斷了一下

private void initSoundPool() {    if(Build.VERSION.SDK_INT>=21){      SoundPool.Builder builder = new SoundPool.Builder();      //設置      builder.setMaxStreams(1);      AudioAttributes attributes = new AudioAttributes.Builder()          .setLegacyStreamType(AudioManager.STREAM_MUSIC)          .build();      builder.setAudioAttributes(attributes);      mSoundPool = builder.build();    }else {      //已經過時,老版本      mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//參數三保留參數    }    //添加音樂    //參數三是音樂池中音樂播放的優先級    mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);  }

初始化振動器:(是一個系統的服務)

mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

搖一搖利用的是加速度傳感器,需要監聽它的變化狀態
SensorManager的注冊也是成對出現的

 @Override  protected void onStart() {    super.onStart();    //4、設置傳感器監聽,加速傳感器    initSensor();  }  @Override  protected void onStop() {    super.onStop();    //解除注冊    mSensorManager.unregisterListener(this);  }  private void initSensor() {    Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    //通過SensorManager實現監聽加速傳感器    mSensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_UI);  }

這里的監聽需要手動去實現(這里是比較不智能的…)
implements SensorEventListener會實現兩個方法

//數據發生變化 @Override  public void onSensorChanged(SensorEvent event) {  }//精度發生變化,傳感器的,該方法用不到  @Override  public void onAccuracyChanged(Sensor sensor, int accuracy) {  }

下面是方法的具體實現:

@Override  public void onSensorChanged(SensorEvent event) {//數據發生變化    Sensor sensor = event.sensor;    int type = sensor.getType();    switch (type){      case Sensor.TYPE_ACCELEROMETER://加速傳感器        float[] values = event.values;        //x,y,z 三個方向        //9.8        float x = values[0];        float y = values[1];        float z = values[2];        if(Math.abs(x)>25||Math.abs(y)>25||Math.abs(z)>25){          //觸發搖一搖          //音樂播放          mSoundPool.play(mSoundPool_id,1,1,0,0,1);          //震動(-1代表只執行一次)          mVibrator.vibrate(new long[]{200,300,400,200},-1);          //動畫執行          initAnimation();        }        break;    }  }

動畫沒什么好解釋的,直接上代碼了

private void initAnimation() {  //up_logo 向上移動,同時有上下震動  AnimationSet set_up = new AnimationSet(true);  TranslateAnimation up_up = new TranslateAnimation(    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點    TranslateAnimation.RELATIVE_TO_SELF,0,//y軸起點    TranslateAnimation.RELATIVE_TO_SELF,-1//y軸終點    );  up_up.setDuration(1000);  TranslateAnimation up_down = new TranslateAnimation(    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點    TranslateAnimation.RELATIVE_TO_SELF,-1,//y軸起點    TranslateAnimation.RELATIVE_TO_SELF,0//y軸終點  );  up_down.setDuration(1000);  //延遲執行set中的某一動畫  up_down.setStartOffset(500);  set_up.addAnimation(up_up);//移動上去;  set_up.addAnimation(up_down);//拉下來  up_logo.startAnimation(set_up);//----------  AnimationSet set_down = new AnimationSet(true);  TranslateAnimation down_down = new TranslateAnimation(    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點    TranslateAnimation.RELATIVE_TO_SELF,0,//y軸起點    TranslateAnimation.RELATIVE_TO_SELF,1//y軸終點  );  down_down.setDuration(1000);  TranslateAnimation down_up = new TranslateAnimation(    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點    TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點    TranslateAnimation.RELATIVE_TO_SELF,1,//y軸起點    TranslateAnimation.RELATIVE_TO_SELF,0//y軸終點  );  down_up.setDuration(1000);  down_up.setStartOffset(500);  set_down.addAnimation(down_down);//向下移動  set_down.addAnimation(down_up);//往上拉動  down_logo.startAnimation(set_down); }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 左贡县| 上饶市| 班戈县| 禄丰县| 普兰县| 遂川县| 任丘市| 原阳县| 岗巴县| 交城县| 纳雍县| 青海省| 章丘市| 武宁县| 塔河县| 大厂| 通州区| 阳高县| 比如县| 扬中市| 大港区| 秭归县| 二手房| 平远县| 洱源县| 富宁县| 裕民县| 景谷| 册亨县| 获嘉县| 板桥市| 会理县| 陆丰市| 临清市| 泊头市| 仙桃市| 雷波县| 盐亭县| 思南县| 陆河县| 禹城市|