Android手机摇一摇的实现SensorEventListener

Posted wzzkaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android手机摇一摇的实现SensorEventListener相关的知识,希望对你有一定的参考价值。

android手机摇一摇的实现SensorEventListener 看实例
package com.example.shakeactivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class ShakeListener implements SensorEventListener {

/* Sensor 说明
* Sensor.TYPE_ACCELEROMETER 加速度感应检測
* Sensor.TYPE_MAGNETIC_FIELD 磁场感应检測
* Sensor.TYPE_ORIENTATION 方位感应检測
* Sensor.TYPE_GYROSCOPE 回转仪感应检測
* Sensor.TYPE_LIGHT 亮度感应检測
* Sensor.TYPE_PRESSURE 压力感应检測
* Sensor.TYPE_TEMPERATURE 温度感应检測
* Sensor.TYPE_PROXIMITY 接近感应检測

* SENSOR_DELAY_FASTEST 最低延迟。一般不是特别敏感的处理不推荐使用,该种模式可能造成手机电力大量消耗。因为传递的为原始数据,算法不处理好将会影响游戏逻辑和UI的性能,所以不推荐使用。

* SENSOR_DELAY_GAME 游戏延迟,一般绝大多数的实时性较高的游戏都使用该级别 * SENSOR_DELAY_NORMAL 标准延迟,对于一般的益智类或EASY级别的游戏能够使用,但过低的採样率可能对一些赛车类游戏有跳帧现象。

* SENSOR_DELAY_UI 用户界面延迟。一般对于屏幕方向自己主动旋转使用,相对节省电能和逻辑处理,一般游戏开发中我们不使用。 */ private static final int FORCE_THRESHOLD = 350; private static final int TIME_THRESHOLD = 100; private static final int SHAKE_TIMEOUT = 500; private static final int SHAKE_DURATION = 1000; private static final int SHAKE_COUNT = 6; private SensorManager mSensorMgr; private float mLastX = -1.0f, mLastY = -1.0f, mLastZ = -1.0f; private long mLastTime; private OnShakeListener mShakeListener; private Context mContext; private int mShakeCount = 0; private long mLastShake; private long mLastForce; public interface OnShakeListener { public void onShake(); //public void onShakeHorizontal(); //public void onShakeVertical(); } public ShakeListener(Context context) { mContext = context; resume(); } public void setOnShakeListener(OnShakeListener listener) { mShakeListener = listener; } public void resume() { mSensorMgr = (SensorManager) mContext .getSystemService(Context.SENSOR_SERVICE); if (mSensorMgr == null) { throw new UnsupportedOperationException("Sensors not supported"); } boolean supported = mSensorMgr.registerListener(this, mSensorMgr .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI); if (!supported) { mSensorMgr.unregisterListener(this); throw new UnsupportedOperationException( "Accelerometer not supported"); } } public void pause() { if (mSensorMgr != null) { mSensorMgr.unregisterListener(this); mSensorMgr = null; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @SuppressWarnings("deprecation") @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) { return; } long now = System.currentTimeMillis(); if ((now - mLastForce) > SHAKE_TIMEOUT) { mShakeCount = 0; } if ((now - mLastTime) > TIME_THRESHOLD) { long diff = now - mLastTime; float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000; if (speed > FORCE_THRESHOLD) { if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) { mLastShake = now; mShakeCount = 0; if (mShakeListener != null) { mShakeListener.onShake(); } } mLastForce = now; } mLastTime = now; mLastX = event.values[SensorManager.DATA_X]; mLastY = event.values[SensorManager.DATA_Y]; mLastZ = event.values[SensorManager.DATA_Z]; } } }

将上面的内容保存为ShakeActivity.java文件,在 MainActivity.java 使用下面方法
ShakeListener mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener() {
public void onShake() {
// action while shaking
setTextView("shaked");
}
});

以上是关于Android手机摇一摇的实现SensorEventListener的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 实现微信摇一摇的功能 Flutter 加速度感应

iOS 摇一摇的实现-备用

cocos2dx 3.2怎么实现类似微信摇一摇

Android学习笔记:摇一摇功能的实现

咸鱼教程Egret实现摇一摇功能

玩转Android之加速度传感器的使用,模仿微信摇一摇