安卓系統(tǒng)中實(shí)現(xiàn)搖一搖畫面振動(dòng)效果的方法
前言
在微信剛流行的時(shí)候,在搖一搖還能用來那啥的時(shí)候,我也曾深更半夜的拿著手機(jī)晃一晃。當(dāng)時(shí)想的最多的就是,我靠,為神馬搖一下需要用這么大的力度,當(dāng)時(shí)我想可能騰訊覺得那是個(gè)人性的設(shè)計(jì),后來才發(fā)覺尼馬重力加速度設(shè)得太高了吧。扯多了,最近項(xiàng)目里需要解決一個(gè)振動(dòng)的問題,因此在學(xué)習(xí)振動(dòng)實(shí)現(xiàn)的過程中,寫了個(gè)demo實(shí)現(xiàn)了搖一搖振動(dòng)的效果,這里記錄一下。
原理
搖一搖功能的基本原理就是:利用手機(jī)的加速度傳感器,當(dāng)加速度到達(dá)某個(gè)值時(shí),觸發(fā)某個(gè)事件,例如手機(jī)振動(dòng)、UI改變等。這里要實(shí)現(xiàn)該功能,首先需要了解一下Android傳感器的使用。
Android傳感器Sensor使用
Android中有多種傳感器,目前Android SDK支持的傳感器包括:加速度傳感器、光線傳感器、陀螺儀傳感器、重力傳感器、方向傳感器、磁場(chǎng)傳感器、壓力傳感器等。但是并不是所有手機(jī)都具有這些傳感器的,因?yàn)閭鞲衅餍枰猰oney,因此廉價(jià)的手機(jī)會(huì)選擇常用的傳感器來添加,而且一些高端機(jī)型則基本上具有大多數(shù)傳感器。
Sensor使用步驟
Android傳感器的使用步驟大致可分為三步:
1. 獲取傳感器管理服對(duì)象 SensorManager。
2. 創(chuàng)建傳感器事件監(jiān)聽類,該類必須實(shí)現(xiàn)android.hardware.SensorEventListener接口。
3. 使用SensorManager.registerListener方法注冊(cè)指定的傳感器。
傳感器事件接口
SensorEventListener接口,該接口的onSensorChanged()和onAccuracyChanged()方法用于處理相應(yīng)的傳感器事件。
public interface SensorEventListener {
/**
* Called when sensor values have changed.
* <p>See {@link android.hardware.SensorManager SensorManager}
* for details on possible sensor types.
* <p>See also {@link android.hardware.SensorEvent SensorEvent}.
*
* <p><b>NOTE:</b> The application doesn't own the
* {@link android.hardware.SensorEvent event}
* object passed as a parameter and therefore cannot hold on to it.
* The object may be part of an internal pool and may be reused by
* the framework.
*
* @param event the {@link android.hardware.SensorEvent SensorEvent}.
*/
public void onSensorChanged(SensorEvent event);
/**
* Called when the accuracy of a sensor has changed.
* <p>See {@link android.hardware.SensorManager SensorManager}
* for details.
*
* @param accuracy The new accuracy of this sensor
*/
public void onAccuracyChanged(Sensor sensor, int accuracy);
}
Android振動(dòng)實(shí)現(xiàn)
Android振動(dòng)效果實(shí)現(xiàn)主要是依靠Vibrator服務(wù),具體調(diào)用方法如下代碼所示:
import android.app.Activity;
import android.app.Service;
import android.os.Vibrator;
public class VibratorHelper {
public static void Vibrate(final Activity activity, long milliseconds) {
Vibrator vibrator = (Vibrator) activity
.getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(milliseconds);
}
public static void Vibrate(final Activity activity, long[] pattern,
boolean isRepeat) {
Vibrator vibrator = (Vibrator) activity
.getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, isRepeat ? 1 : -1);
}
}
同時(shí),還需要在AndroidManifest.xml里增加振動(dòng)權(quán)限:
<uses-permission android:name="android.permission.VIBRATE"/>
解釋一下Vibrate方法的參數(shù):
1. long milliseconds:振動(dòng)的時(shí)長(zhǎng),單位是毫秒。
2. long[] pattern:自定義振動(dòng)模式。數(shù)組中數(shù)字的含義依次是[靜止時(shí)長(zhǎng), 振動(dòng)時(shí)長(zhǎng), 靜止時(shí)長(zhǎng), 振動(dòng)時(shí)長(zhǎng), ......]。振動(dòng)時(shí)長(zhǎng)的單位是毫秒。
3. repeat:是否重復(fù)振動(dòng),1為重復(fù),-1為只振動(dòng)一次。
搖一搖振動(dòng)Demo實(shí)現(xiàn)
好了,了解了搖一搖需要借助加速度傳感器,振動(dòng)需要借助Vibrator服務(wù),那就直接來寫代碼了。MainActivity類實(shí)現(xiàn)如下:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity {
private SensorManager sensorManager;
private SensorEventListener shakeListener;
private AlertDialog.Builder dialogBuilder;
private boolean isRefresh = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
shakeListener = new ShakeSensorListener();
dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setPositiveButton("確定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
isRefresh = false;
dialog.cancel();
}
}).setMessage("搖到了一個(gè)漂亮妹子!").create();
}
@Override
protected void onResume() {
sensorManager.registerListener(shakeListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
super.onResume();
}
@Override
protected void onPause() {
// acitivity后臺(tái)時(shí)取消監(jiān)聽
sensorManager.unregisterListener(shakeListener);
super.onPause();
}
private class ShakeSensorListener implements SensorEventListener {
private static final int ACCELERATE_VALUE = 20;
@Override
public void onSensorChanged(SensorEvent event) {
// Log.e("zhengyi.wzy", "type is :" + event.sensor.getType());
// 判斷是否處于刷新狀態(tài)(例如微信中的查找附近人)
if (isRefresh) {
return;
}
float[] values = event.values;
/**
* 一般在這三個(gè)方向的重力加速度達(dá)到20就達(dá)到了搖晃手機(jī)的狀態(tài) x : x軸方向的重力加速度,向右為正 y :
* y軸方向的重力加速度,向前為正 z : z軸方向的重力加速度,向上為正
*/
float x = Math.abs(values[0]);
float y = Math.abs(values[1]);
float z = Math.abs(values[2]);
Log.e("zhengyi.wzy", "x is :" + x + " y is :" + y + " z is :" + z);
if (x >= ACCELERATE_VALUE || y >= ACCELERATE_VALUE
|| z >= ACCELERATE_VALUE) {
Toast.makeText(
MainActivity.this,
"accelerate speed :"
+ (x >= ACCELERATE_VALUE ? x
: y >= ACCELERATE_VALUE ? y : z),
Toast.LENGTH_SHORT).show();
VibratorHelper.Vibrate(MainActivity.this, 300);
isRefresh = true;
dialogBuilder.show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
}
效果圖:

- Android利用傳感器實(shí)現(xiàn)微信搖一搖功能
- Android 微信搖一搖功能實(shí)現(xiàn)詳細(xì)介紹
- IOS 實(shí)現(xiàn)搖一搖的操作
- iOS實(shí)現(xiàn)微信朋友圈與搖一搖功能
- javascript html5搖一搖功能的實(shí)現(xiàn)
- 分享網(wǎng)頁檢測(cè)搖一搖實(shí)例代碼
- 使用PHP實(shí)現(xiàn)微信搖一搖周邊紅包
- php官方微信接口大全(微信支付、微信紅包、微信搖一搖、微信小店)
- HTML5使用DeviceOrientation實(shí)現(xiàn)搖一搖功能
- Android實(shí)現(xiàn)搖一搖功能
相關(guān)文章
Spring基于Aop實(shí)現(xiàn)事務(wù)管理流程詳細(xì)講解
這篇文章主要介紹了Spring基于Aop實(shí)現(xiàn)事務(wù)管理流程,事務(wù)管理對(duì)于企業(yè)應(yīng)用來說是至關(guān)重要的,即使出現(xiàn)異常情況,它也可以保證數(shù)據(jù)的一致性,感興趣想要詳細(xì)了解可以參考下文2023-05-05
基于@JsonSerialize和@JsonInclude注解使用方法
這篇文章主要介紹了@JsonSerialize和@JsonInclude注解使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹的原理與實(shí)現(xiàn)
平衡樹(Balance Tree,BT) 指的是,任意節(jié)點(diǎn)的子樹的高度差都小于等于1。常見的符合平衡樹的有,B樹(多路平衡搜索樹)、AVL樹(二叉平衡搜索樹)等。本文將詳細(xì)介紹平衡二叉樹的概念和實(shí)現(xiàn)原理以及它的實(shí)現(xiàn)2022-01-01
SpringCloud實(shí)戰(zhàn)之Zuul網(wǎng)關(guān)服務(wù)
服務(wù)網(wǎng)關(guān)是分布式架構(gòu)中不可缺少的組成部分,是外部網(wǎng)絡(luò)和內(nèi)部服務(wù)之間的屏障。這篇文章主要介紹了SpringCloud實(shí)戰(zhàn)之Zuul網(wǎng)關(guān)服務(wù)。一起跟隨小編過來看看吧2018-05-05
java后臺(tái)實(shí)現(xiàn)支付寶對(duì)賬功能的示例代碼
這篇文章主要介紹了java后臺(tái)實(shí)現(xiàn)支付寶對(duì)賬功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
一文詳解Spring事務(wù)的實(shí)現(xiàn)與本質(zhì)
這篇文章主要介紹了Spring中事務(wù)的兩種實(shí)現(xiàn)方式:聲明式事務(wù)、編程式事務(wù)以及他們的本質(zhì)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04
java使用BeanUtils.copyProperties方法對(duì)象復(fù)制同名字段類型不同賦值為空問題解決方案
這篇文章主要給大家介紹了關(guān)于java使用BeanUtils.copyProperties方法對(duì)象復(fù)制同名字段類型不同賦值為空問題的解決方案,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot Starter機(jī)制及整合tomcat的實(shí)現(xiàn),我們知道SpringBoot自己在“后臺(tái)”幫我們配置了很多原本需要我們手動(dòng)去的東西,至于這個(gè)“后臺(tái)”是啥,就是Starter機(jī)制2022-09-09
Java詳細(xì)講解Math和Random類中有哪些常用方法
Math類位于java.lang包中,包含很多用于科學(xué)計(jì)算的類方法,這些方法可以直接通過類名調(diào)用。Random類獲取隨機(jī)數(shù),位于java.util包中,本篇帶你了解它們的常用方法2022-05-05

