安卓體系中完成搖一搖畫面振動後果的辦法。本站提示廣大學習愛好者:(安卓體系中完成搖一搖畫面振動後果的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是安卓體系中完成搖一搖畫面振動後果的辦法正文
媒介
在微信剛風行的時刻,在搖一搖還能用來那啥的時刻,我也曾深更子夜的拿著手機晃一晃。其時想的最多的就是,我靠,為神馬搖一下須要用這麼年夜的力度,其時我想能夠騰訊認為那是小我性的設計,後來才覺察尼馬重力加快度設得太高了吧。扯多了,比來項目裡須要處理一個振動的成績,是以在進修振動完成的進程中,寫了個demo完成了搖一搖振動的後果,這裡記載一下。
道理
搖一搖功效的根本道理就是:應用手機的加快度傳感器,當加快度達到某個值時,觸發某個事宜,例如手機振動、UI轉變等。這裡要完成該功效,起首須要懂得一下Android傳感器的應用。
Android傳感器Sensor應用
Android中有多種傳感器,今朝Android SDK支撐的傳感器包含:加快度傳感器、光線傳感器、陀螺儀傳感器、重力傳感器、偏向傳感器、磁場傳感器、壓力傳感器等。然則其實不是一切手機都具有這些傳感器的,由於傳感器須要money,是以便宜的手機遇選擇經常使用的傳感器來添加,並且一些高端機型則根本上具有年夜多半傳感器。
Sensor應用步調
Android傳感器的應用步調年夜致可分為三步:
1. 獲得傳感器治理服對象 SensorManager。
2. 創立傳感器事宜監聽類,該類必需完成android.hardware.SensorEventListener接口。
3. 應用SensorManager.registerListener辦法注冊指定的傳感器。
傳感器事宜接口
SensorEventListener接口,該接口的onSensorChanged()和onAccuracyChanged()辦法用於處置響應的傳感器事宜。
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振動完成
Android振動後果完成重要是依附Vibrator辦事,詳細挪用辦法以下代碼所示:
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);
}
}
同時,還須要在AndroidManifest.xml裡增長振動權限:
<uses-permission android:name="android.permission.VIBRATE"/>
說明一下Vibrate辦法的參數:
1. long milliseconds:振動的時長,單元是毫秒。
2. long[] pattern:自界說振動形式。數組中數字的寄義順次是[運動時長, 振動時長, 運動時長, 振動時長, ......]。振動時長的單元是毫秒。
3. repeat:能否反復振動,1為反復,-1為只振動一次。
搖一搖振動Demo完成
好了,懂得了搖一搖須要借助加快度傳感器,振動須要借助Vibrator辦事,那就直接來寫代碼了。MainActivity類完成以下:
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("搖到了一個英俊妹子!").create();
}
@Override
protected void onResume() {
sensorManager.registerListener(shakeListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
super.onResume();
}
@Override
protected void onPause() {
// acitivity後台時撤消監聽
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());
// 斷定能否處於刷新狀況(例如微信中的查找鄰近人)
if (isRefresh) {
return;
}
float[] values = event.values;
/**
* 普通在這三個偏向的重力加快度到達20就到達了搖擺手機的狀況 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
}
}
}
後果圖: