Android摇一摇震动效果Demo
前言
原理
Android传感器Sensor使用
Sensor使用步骤
传感器事件接口
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震动实现
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方法的参数:
摇一摇震动Demo实现
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 } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。