Android Studio Gyroscope examples?

半世苍凉 提交于 2019-12-25 03:32:41

问题


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!


回答1:


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."



来源:https://stackoverflow.com/questions/30228586/android-studio-gyroscope-examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!