亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

傳感器

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

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

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

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

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

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

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

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

實現(xiàn)之前先來看看手機上的傳感器有哪些?(此處可以略過…)
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("傳感器名稱: "+sensor.getName()+", 傳感器生產(chǎn)廠商: "+sensor.getVendor()+"\n");
  }
  sensor.setText(builder.toString());

驚奇的發(fā)現(xiàn)小米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的時候,發(fā)現(xiàn)new SoundPool已經(jīng)不推薦使用了,新版的(API>21)使用Builder構(gòu)建,所以在這里使用版本進行判斷了一下

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)過時,老版本
   mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//參數(shù)三保留參數(shù)
  }
  //添加音樂
  //參數(shù)三是音樂池中音樂播放的優(yōu)先級
  mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);
 }

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

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

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

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

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

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

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

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

 }

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

 }

下面是方法的具體實現(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 三個方向
    //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ā)搖一搖
     //音樂播放
     mSoundPool.play(mSoundPool_id,1,1,0,0,1);

     //震動(-1代表只執(zhí)行一次)
     mVibrator.vibrate(new long[]{200,300,400,200},-1);
     //動畫執(zhí)行
     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);

 //延遲執(zhí)行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);

 }

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

相關(guān)文章

最新評論