Adnroid using Accelerometer

http://code.tutsplus.com/tutorials/using-the-accelerometer-on-android--mobile-22125

public class Main extends Activity implements SensorEventListener {
    private SensorManager senSensorManager;
    private Sensor senAccelerometer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];

            long curTime = System.currentTimeMillis();

            if ((curTime - lastUpdate) > 100) {
                long diffTime = (curTime - lastUpdate);
                lastUpdate = curTime;

                float speed = Math.abs(x + y + z - last_x - last_y - last_z)/ diffTime * 10000;

                if (speed > SHAKE_THRESHOLD) {

                }

                last_x = x;
                last_y = y;
                last_z = z;
            }
        }
    }
}
时间: 2024-07-30 13:51:06

Adnroid using Accelerometer的相关文章

coocs2d-html5在使用cocoseditor时调用设备的accelerometer来使用重力感应

在使用大牛touchsnow开发的插件cocoseditor开发游戏时遇到了一些问题,然后就试着解决,最近想试下coocs2d-html5能否使用重力感应,发现是可以的,不过这个只能在移动真机上测试,电脑上的模拟器是不行的, 首先需要在onDidloadFromCCB()方法内设置可以使用Accelerometer: this.rootNode.onAccelerometer = function (event) { this.controller.onAccelerometer(event);

STM32 F4 SPI Accelerometer

STM32 F4 SPI Accelerometer

adnroid仿miui的dialog

先来看下效果图: 其中show和dismiss的时候有动画效果. 原先试过使用PopupWindow来做,但是使用的时候不是那么舒服,毕竟不是dialog嘛. 所以这次尝试还是使用dialog来做 ,很多地方是引用了系统源码(源码是最好的老师) 首先看CustomDialog.java的构造函数: protected CustomDialog(Context context) { this(context, R.style.CustomDialog); } protected CustomDia

Adnroid 两种下拉刷新 方式的实现 sina刷新 gmail刷新

sina刷新 这种下拉刷新的方式是比较简单的.上个图: 这种刷新方式的思路是这样的: 首先是需要一个HeaderVIew也就是刷新时头部所显示出来的数据.这个view的布局随你,长啥样自己定夺. 其他不是特别重要,重要的是用户触摸事件的捕捉,看到github上的大神的一些方法是比较正规的,我就自己用自己的方法尝试,主要是捕捉到用户的点击事件来计算用户所触摸到的位置然后来更新头部布局的位置. 这个重要的代码贴出来: case MotionEvent.ACTION_MOVE: currentY =

[转]A Guide To using IMU (Accelerometer and Gyroscope Devices) in Embedded Applications.

原文地址http://www.starlino.com/imu_guide.html Introduction There’s now a FRENCH translation of this article in PDF. Thanks to Daniel Le Guern! This guide is intended to everyone interested in inertial MEMS (Micro-Electro-Mechanical Systems) sensors, in

加速度传感器Accelerometer

Android系统为传感器支持强大的管理服务,开发传感器应用的步骤如下: (1)调用Context的 getSystemService(Context.SENSOR_SERVICE)方法获取Sensor Manager对象,SensorManager对象代表系统的传感器管理服务. (2)调用SensorManager的getDefaultSensor(int type)方法来获取制定类型的传感器 (3)通常选择在Activity 的onResume()方法中调用SensorManager 的 r

Cardboard Talk02 Accelerometer

操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Android studio 3.0.0 | Cardboard 1.0 在深入讨论具体实现之前,有必要了解一下Android系统的 IMU 传感器与坐标系之间的关系及关系转换,才不至于在后面实现.应用中搞混坐标系.在此借机会翻译 NVDIA 的一篇文章 Tegra Android Accelerometer Whitepaper ,内容如有不当之处请指教. Introduction 移动便携设备可以进行多方向的旋转.

Xamarin Essentials教程使用加速度传感器Accelerometer

Xamarin Essentials教程使用加速度传感器Accelerometer 加速度传感器是一种能够测量加速度的传感器,用于检测设备状态的改变.在Xamarin中,如果开发者想要使用加速度传感器,可以使用Xamarin.Essentials组件提供的静态类Accelerometer.该类允许开发者监视设备的加速传感器的三维空间数据. 原文地址:https://www.cnblogs.com/daxueba-ITdaren/p/9316079.html

03模块-accelerometer

accelerometer模块主要用于管理设备加速度传感器. 用于获取设备加速度信息,包括x(屏幕水平方向).y(垂直屏幕水平方向).z(垂直屏幕平面方向)三个方向的加速度信息. 该模块有三个方法 1.getCurrentAcceleration: 获取当前设备的加速度信息 2.watchAcceleration: 监听设备加速度变化信息 3.clearWatch: 关闭监听设备加速度信息 getCurrentAcceleration方法可以获取当前设备的加速度信息 <!DOCTYPE html