Android利用傳感器仿微信搖一搖功能
傳感器
簡單的介紹一下傳感器:
就是設(shè)備用來感知周邊環(huán)境變化的硬件。
Android中的傳感器包含在傳感器框架中,屬于android.hardware.*(硬件部分)
傳感器框架主要包含四個(gè)部分:
① SensorManager:用來獲取傳感器的入口,它是一個(gè)系統(tǒng)的服務(wù),還可以為傳感器注冊與取消注冊監(jiān)聽
② Sensor: 具體的傳感器,包含了傳感器的名字,類型,采樣率
③ SensorEvent:傳感器事件,包含了傳感器采集回來的數(shù)據(jù),傳感器的精度
④ SensorEventListener:傳感器的監(jiān)聽,主要監(jiān)測傳感器數(shù)據(jù)變化,精度變化…
Android播放音頻系統(tǒng)提供了兩種方式
① MediaPlayer 播放常規(guī)的音頻,視頻,通常用在播放器上
② SoundPool 聲音池,通常用在小而頻繁播放的音樂,需要同時(shí)播放多個(gè)音樂的
VIBRATE 所震動(dòng)傳感器需要添加權(quán)限
<uses-permission android:name="android.permission.VIBRATE"/>
實(shí)現(xiàn)之前先來看看手機(jī)上的傳感器有哪些?(此處可以略過…)
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個(gè)傳感器,可以哈…厲害了
好了,好了接下來言歸正傳—微信搖一搖
界面的話三張圖片你的層疊,RelativeLayout進(jìn)行布局

在onCreate中封裝的方法
//搖一搖---->加速傳感器 //1、初始化控件 initView(); //2、初始化音樂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)過時(shí),老版本
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//參數(shù)三保留參數(shù)
}
//添加音樂
//參數(shù)三是音樂池中音樂播放的優(yōu)先級
mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);
}
初始化振動(dòng)器:(是一個(gè)系統(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實(shí)現(xiàn)監(jiān)聽加速傳感器
mSensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_UI);
}
這里的監(jiān)聽需要手動(dòng)去實(shí)現(xiàn)(這里是比較不智能的…)
implements SensorEventListener會實(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ā)搖一搖
//音樂播放
mSoundPool.play(mSoundPool_id,1,1,0,0,1);
//震動(dòng)(-1代表只執(zhí)行一次)
mVibrator.vibrate(new long[]{200,300,400,200},-1);
//動(dòng)畫執(zhí)行
initAnimation();
}
break;
}
}
動(dòng)畫沒什么好解釋的,直接上代碼了
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)畫
up_down.setStartOffset(500);
set_up.addAnimation(up_up);//移動(dòng)上去;
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軸起點(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);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)微信搖一搖功能
- android重力感應(yīng)開發(fā)之微信搖一搖功能
- Android編程使用加速度傳感器實(shí)現(xiàn)搖一搖功能及優(yōu)化的方法詳解
- Android實(shí)現(xiàn)iPhone晃動(dòng)撤銷輸入功能 Android仿微信搖一搖功能
- Android實(shí)現(xiàn)搖一搖功能
- Android利用傳感器實(shí)現(xiàn)微信搖一搖功能
- Android 微信搖一搖功能實(shí)現(xiàn)詳細(xì)介紹
- android 類似微信的搖一搖功能實(shí)現(xiàn)思路及代碼
- Android實(shí)現(xiàn)搖一搖簡單功能
相關(guān)文章
Android通過HttpURLConnection和HttpClient接口實(shí)現(xiàn)網(wǎng)絡(luò)編程
這篇文章主要介紹了Android通過HttpURLConnection和HttpClient接口實(shí)現(xiàn)網(wǎng)絡(luò)編程的相關(guān)資料,需要的朋友可以參考下2015-02-02
Android BottomSheet實(shí)現(xiàn)可拉伸控件
這篇文章主要為大家詳細(xì)介紹了Android BottomSheet實(shí)現(xiàn)可拉伸控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼,需要的朋友可以參考下。2011-05-05
解決android6.0以上不能讀取外部存儲權(quán)限的問題
今天小編就為大家分享一篇解決android6.0以上不能讀取外部存儲權(quán)限的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Android開發(fā)自定義短信驗(yàn)證碼實(shí)現(xiàn)過程詳解
這篇文章主要為大家介紹了Android開發(fā)自定義短信驗(yàn)證碼實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Android Studio實(shí)現(xiàn)簡單音樂播放功能的示例代碼
這篇文章主要介紹了Android Studio實(shí)現(xiàn)簡單音樂播放功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
flutter實(shí)現(xiàn)點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)點(diǎn)擊事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08

