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

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-23 18:29:37
字體:
供稿:網(wǎng)友

傳感器

簡單的介紹一下傳感器:
就是設(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進行布局

Android,微信,搖一搖

在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)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛山市| 邯郸县| 边坝县| 德化县| 新竹市| 绥江县| 郯城县| 红安县| 玉树县| 宣城市| 武穴市| 福安市| 蛟河市| 建德市| 托克托县| 三门峡市| 手机| 平南县| 新宁县| 五原县| 乐陵市| 商丘市| 如皋市| 奉化市| 舒城县| 南充市| 泰来县| 桂阳县| 莱芜市| 区。| 麻阳| 紫云| 二手房| 武穴市| 平罗县| 抚远县| 龙南县| 牟定县| 怀安县| 商都县| 清原|