Android Studio陀螺仪示例? [英] Android Studio Gyroscope examples?

查看:232
本文介绍了Android Studio陀螺仪示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个应用程序,该设备在设备倾斜到一定程度时会产生预期的效果.

我已经看过并成功启用了加速度计,但这并没有给我想要的效果.就像我说的那样,我希望设备仅在达到一定程度(例如90度)时才能做自己想要的事情.

我有以下代码,但这仅在设备倾斜得足够快时才有效:

  @Override公共无效onSensorChanged(SensorEvent sensorEvent){传感器mySensor = sensorEvent.sensor;如果(mySensor.getType()== Sensor.TYPE_GYROSCOPE){浮点数x = sensorEvent.values [0];浮点y = sensorEvent.values [1];浮点数z = sensorEvent.values [2];long currTime = System.currentTimeMillis();如果((currTime-lastUpdate)> 100){长diffTime =(currTime-lastUpdate);lastUpdate = currTime;浮点速度= Math.abs(x + y + z-last_x-last_y-last_z)/diffTime * 10000;如果(速度> SHAKE_THRESHOLD&&!sound.isPlaying()){sound.start();}last_x = x;last_y = y;last_z = z;}}} 

此代码最初是与加速度计一起使用的,我只是将 Sensor.TYPE_ACCELEROMETER 更改为 Sensor.TYPE_GYROSCOPE ,希望这可以告诉我该怎么做.

我可以做这样的事情吗?

  if(x> 90 || y> 90 || z> 90&&!sound.isPlaying()){sound.start();} 

代替我的

  if(速度> SHAKE_THRESHOLD&&!sound.isPlaying()){sound.start();} 

我已经尝试寻找如何执行此类操作的示例,但即使在Android开发人员上也找不到任何内容...

我想要一些简单的东西,它可以让我知道设备何时达到一定程度或弧度,速度无关紧要.

任何帮助都会很棒.

谢谢!

内森

我已经做到了:

  if(z> 5 || z< -5 || x> 5 || x< -5&&!sound.isPlaying()){sound.start();}否则,如果(z == 4 || x == 4){sound.stop();声音= MediaPlayer.create(this,R.raw.sound);} 

这在一定程度上可行.一旦Z和X值超过阈值"5",便会播放声音.但是,如果我将设备留在该位置,则在将设备返回到初始位置后,它会再次激活声音,因为它仍处于"5"阈值之内.

这不是我想要的,所以我想尝试一些不同的方法:

  if(z == 5 || z == -5 || x == 5 || x == -5&&!sound.isPlaying()){sound.start();}否则,如果(z == 4 || x == 4){sound.stop();声音= MediaPlayer.create(this,R.raw.sound);} 

这根本不起作用.

我希望设备能够检测到它何时超过了阈值"5",但是我不希望它能够移动并且仍然在阈值之内再次发出声音.

基本上,我希望设备能够在线穿过时播放声音,而不是在线之后的任何点播放声音.我发现让它寻找数字 == 5 何时会执行,但似乎无法做到这一点.

停止功能也不起作用.如果我完全使用 == ,它将不起作用.我可以通过另一种方式来编写Java能够识别的代码吗?

请记住,我希望它能够知道何时交叉点,而不是何时输入区域.

干杯!

解决方案

加速度计:以m/s2为单位测量在三个物理轴(x,y和z)上施加到设备的加速力,包括引力."

方向:测量设备围绕所有三个物理轴(x,y,z)旋转的角度.从API级别3开始,您可以使用重力传感器和地磁场传感器与getRotationMatrix()方法结合使用."

I'm trying to build an application which does a desired affect when the device is tilted to a certain degree.

I've taken a look at, and successfully enabled, an accelerometer, but this doesn't give me the desired affect. Like I said, I wish the device to do what I want it to, only when the device has achieved a certain degree, say 90 degrees.

I've got the following code, but this only works when the device is tilted fast enough:

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor mySensor = sensorEvent.sensor;
    if (mySensor.getType() == Sensor.TYPE_GYROSCOPE) {
        float x = sensorEvent.values[0];
        float y = sensorEvent.values[1];
        float z = sensorEvent.values[2];

        long currTime = System.currentTimeMillis();

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

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

            if (speed > SHAKE_THRESHOLD && !sound.isPlaying()) {
                sound.start();
            }

            last_x = x;
            last_y = y;
            last_z = z;
        }
    }
}

This code was originally used with an accelerometer, I just changed the Sensor.TYPE_ACCELEROMETER to Sensor.TYPE_GYROSCOPE, hoping this would reveal to me what to do.

Could I possibly do somethings like this?

if (x > 90 || y > 90 || z > 90 && !sound.isPlaying()) {
    sound.start();
}

In place of my

if (speed > SHAKE_THRESHOLD && !sound.isPlaying()) {
    sound.start();
}

I've tried looking for examples of how to do something like this, but was unable to find anything, even on Android Developers...

I want something simple which will allow me to tell when the device has reached a certain degree or radian, speed does not matter.

Any help would be wonderful.

Thanks!

Nathan

EDIT:

I've done this:

 if (z > 5 || z < -5 || x > 5 || x < -5 && !sound.isPlaying()) {
     sound.start();
 } else if ( z == 4 || x == 4) {
     sound.stop();
     sound = MediaPlayer.create(this, R.raw.sound);
 }

And this works to an extent. Once the Z and X values have crossed the threshold of '5', then the sound will play. But, if I leave the device in that position, then, upon returning the device back into starting position activates the sound again, because it is still within the '5' threshold.

This is not what I was looking for, so I figured I would try something a little different:

if (z == 5 || z == -5 || x == 5 || x == -5 && !sound.isPlaying()) {
    sound.start();
} else if ( z == 4 || x == 4) {
    sound.stop();
    sound = MediaPlayer.create(this, R.raw.sound);
}

This doesn't work at all.

I want the device to be able to detect when it has crossed the threshold of '5', but I do not want it to be able to start the sound again if it is moved and still inside of the threshold.

Basically, I want the device to be able to play the sound when the line is crossed, and not when it is any point after the line. I figured that having it look for when the number == 5 would do it, but it can't seem to do that.

The stop function doesn't work at all either. If I use == at all, it doesn't work. Is there another way I can code this that Java will be able to recognize?

Remember, I want it to be able to know when the point is crossed, not when the area is entered.

Cheers!

解决方案

Accelerometer: "Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity."

Orientation: "Measures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method."

这篇关于Android Studio陀螺仪示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆