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

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

 更新時(shí)間:2021年01月26日 15:36:42   作者:小小時(shí)光機(jī)  
這篇文章主要為大家詳細(xì)介紹了Android利用傳感器仿微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

傳感器

簡(jiǎn)單的介紹一下傳感器:
就是設(shè)備用來(lái)感知周邊環(huán)境變化的硬件。

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

傳感器框架主要包含四個(gè)部分:

① SensorManager:用來(lái)獲取傳感器的入口,它是一個(gè)系統(tǒng)的服務(wù),還可以為傳感器注冊(cè)與取消注冊(cè)監(jiān)聽(tīng)
② Sensor: 具體的傳感器,包含了傳感器的名字,類(lèi)型,采樣率
③ SensorEvent:傳感器事件,包含了傳感器采集回來(lái)的數(shù)據(jù),傳感器的精度
④ SensorEventListener:傳感器的監(jiān)聽(tīng),主要監(jiān)測(cè)傳感器數(shù)據(jù)變化,精度變化…

Android播放音頻系統(tǒng)提供了兩種方式

① MediaPlayer 播放常規(guī)的音頻,視頻,通常用在播放器上
② SoundPool 聲音池,通常用在小而頻繁播放的音樂(lè),需要同時(shí)播放多個(gè)音樂(lè)的

VIBRATE 所震動(dòng)傳感器需要添加權(quán)限

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

實(shí)現(xiàn)之前先來(lái)看看手機(jī)上的傳感器有哪些?(此處可以略過(guò)…)
onCreat();中設(shè)置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("傳感器總數(shù): "+sensorList.size()+"\n");
  for (int i = 0; i < sensorList.size(); i++) {
   Sensor sensor = sensorList.get(i);
   builder.append("傳感器名稱(chēng): "+sensor.getName()+", 傳感器生產(chǎn)廠商: "+sensor.getVendor()+"\n");
  }
  sensor.setText(builder.toString());

驚奇的發(fā)現(xiàn)小米5上竟然有41個(gè)傳感器,可以哈…厲害了
好了,好了接下來(lái)言歸正傳—微信搖一搖

界面的話三張圖片你的層疊,RelativeLayout進(jìn)行布局

在onCreate中封裝的方法

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

上面蓋的兩張圖片初始化

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

在初始化SoundPool的時(shí)候,發(fā)現(xiàn)new SoundPool已經(jīng)不推薦使用了,新版的(API>21)使用Builder構(gòu)建,所以在這里使用版本進(jìn)行判斷了一下

private void initSoundPool() {
  if(Build.VERSION.SDK_INT>=21){
   SoundPool.Builder builder = new SoundPool.Builder();
   //設(shè)置
   builder.setMaxStreams(1);
   AudioAttributes attributes = new AudioAttributes.Builder()
     .setLegacyStreamType(AudioManager.STREAM_MUSIC)
     .build();

   builder.setAudioAttributes(attributes);

   mSoundPool = builder.build();
  }else {
   //已經(jīng)過(guò)時(shí),老版本
   mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//參數(shù)三保留參數(shù)
  }
  //添加音樂(lè)
  //參數(shù)三是音樂(lè)池中音樂(lè)播放的優(yōu)先級(jí)
  mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);
 }

初始化振動(dòng)器:(是一個(gè)系統(tǒng)的服務(wù))

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

搖一搖利用的是加速度傳感器,需要監(jiān)聽(tīng)它的變化狀態(tài)
SensorManager的注冊(cè)也是成對(duì)出現(xiàn)的

 @Override
 protected void onStart() {
  super.onStart();
  //4、設(shè)置傳感器監(jiān)聽(tīng),加速傳感器
  initSensor();
 }

 @Override
 protected void onStop() {
  super.onStop();
  //解除注冊(cè)
  mSensorManager.unregisterListener(this);
 }

 private void initSensor() {
  Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  //通過(guò)SensorManager實(shí)現(xiàn)監(jiān)聽(tīng)加速傳感器
  mSensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_UI);
 }

這里的監(jiān)聽(tīng)需要手動(dòng)去實(shí)現(xiàn)(這里是比較不智能的…)
implements SensorEventListener會(huì)實(shí)現(xiàn)兩個(gè)方法

//數(shù)據(jù)發(fā)生變化
 @Override
 public void onSensorChanged(SensorEvent event) {

 }

//精度發(fā)生變化,傳感器的,該方法用不到
 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {

 }

下面是方法的具體實(shí)現(xiàn):

@Override
 public void onSensorChanged(SensorEvent event) {//數(shù)據(jù)發(fā)生變化
  Sensor sensor = event.sensor;
  int type = sensor.getType();
  switch (type){
   case Sensor.TYPE_ACCELEROMETER://加速傳感器

    float[] values = event.values;
    //x,y,z 三個(gè)方向
    //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){
     //觸發(fā)搖一搖
     //音樂(lè)播放
     mSoundPool.play(mSoundPool_id,1,1,0,0,1);

     //震動(dòng)(-1代表只執(zhí)行一次)
     mVibrator.vibrate(new long[]{200,300,400,200},-1);
     //動(dòng)畫(huà)執(zhí)行
     initAnimation();
    }
    break;
  }
 }

動(dòng)畫(huà)沒(méi)什么好解釋的,直接上代碼了

private void initAnimation() {
 //up_logo 向上移動(dòng),同時(shí)有上下震動(dòng)
 AnimationSet set_up = new AnimationSet(true);
 TranslateAnimation up_up = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0,//y軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,-1//y軸終點(diǎn)
  );

 up_up.setDuration(1000);

 TranslateAnimation up_down = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,-1,//y軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0//y軸終點(diǎn)
 );
 up_down.setDuration(1000);

 //延遲執(zhí)行set中的某一動(dòng)畫(huà)
 up_down.setStartOffset(500);
 set_up.addAnimation(up_up);//移動(dòng)上去;
 set_up.addAnimation(up_down);//拉下來(lái)
 up_logo.startAnimation(set_up);

//----------

 AnimationSet set_down = new AnimationSet(true);
 TranslateAnimation down_down = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0,//y軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,1//y軸終點(diǎn)
 );
 down_down.setDuration(1000);
 TranslateAnimation down_up = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0,//x軸終點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,1,//y軸起點(diǎn)
  TranslateAnimation.RELATIVE_TO_SELF,0//y軸終點(diǎn)
 );
 down_up.setDuration(1000);
 down_up.setStartOffset(500);
 set_down.addAnimation(down_down);//向下移動(dòng)
 set_down.addAnimation(down_up);//往上拉動(dòng)
 down_logo.startAnimation(set_down);

 }

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

相關(guān)文章

最新評(píng)論