本文整理匯總了Java中android.hardware.SensorEvent類的典型用法代碼示例。如果您正苦於以下問題:Java SensorEvent類的具體用法?Java SensorEvent怎麽用?Java SensorEvent使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SensorEvent類屬於android.hardware包,在下文中一共展示了SensorEvent類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
final float value = event.values[0];
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
updateTemperatureDisplay(value);
} else if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
updateBarometerDisplay(value);
}
}
示例2: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
// 傳感器信息改變時執行該方法
float[] values = event.values;
float x = values[0]; // x軸方向的重力加速度,向右為正
float y = values[1]; // y軸方向的重力加速度,向前為正
float z = values[2]; // z軸方向的重力加速度,向上為正
// Log.i("xlight", "x軸方向的重力加速度" + x + ";y軸方向的重力加速度" + y + ";z軸方向的重力加速度" + z);
// 一般在這三個方向的重力加速度達到40就達到了搖晃手機的狀態。
//Logger.i("shake", "x = " + Math.abs(x) + ",y = " + y + ",z = " + z);
int medumValue = 18;// 三星 i9250怎麽晃都不會超過20,沒辦法,隻設置19了
if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
if (!UserUtils.isLogin(SlidingMenuMainActivity.this) || null == mShakeInfo) {
return;
}
vibrator.vibrate(200);
Message msg = new Message();
msg.what = SENSOR_SHAKE;
handler.sendMessage(msg);
}
}
示例3: accelerometerControl
import android.hardware.SensorEvent; //導入依賴的package包/類
private void accelerometerControl(SensorEvent event) {
float xSens;
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
xSens = event.values[1];
// if(xSens > mAccelerometerThreshold){
// mMove = true;
// mMoveLeft = false;
// mTouch = true;
// return;
// }
// if(xSens < -mAccelerometerThreshold){
// mMove = true;
// mTouch = true;
// mMoveLeft = true;
// return;
// }
// mMove = false;
// if(mFire == false){
// mTouch = false;
// }
}
示例4: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.timestamp - mLastTimestamp < MIN_TIME_BETWEEN_SAMPLES_MS) {
return;
}
Assertions.assertNotNull(mTimestamps);
Assertions.assertNotNull(mMagnitudes);
float ax = sensorEvent.values[0];
float ay = sensorEvent.values[1];
float az = sensorEvent.values[2];
mLastTimestamp = sensorEvent.timestamp;
mTimestamps[mCurrentIndex] = sensorEvent.timestamp;
mMagnitudes[mCurrentIndex] = Math.sqrt(ax * ax + ay * ay + az * az);
maybeDispatchShake(sensorEvent.timestamp);
mCurrentIndex = (mCurrentIndex + 1) % MAX_SAMPLES;
}
示例5: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (x<5 && x>-5 && y > 5)
mOrientation = Surface.ROTATION_0; // portrait
else if (x<-5 && y<5 && y>-5)
mOrientation = Surface.ROTATION_270; // right
else if (x<5 && x>-5 && y<-5)
mOrientation = Surface.ROTATION_180; // upside down
else if (x>5 && y<5 && y>-5)
mOrientation = Surface.ROTATION_90; // left
if (mListener != null) {
mListener.orientationEvent();
}
}
示例6: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.timestamp - mLastTimestamp < MIN_TIME_BETWEEN_SAMPLES_MS) {
return;
}
float ax = sensorEvent.values[0];
float ay = sensorEvent.values[1];
float az = sensorEvent.values[2];
mLastTimestamp = sensorEvent.timestamp;
mTimestamps[mCurrentIndex] = sensorEvent.timestamp;
mMagnitudes[mCurrentIndex] = Math.sqrt(ax * ax + ay * ay + az * az);
maybeDispatchShake(sensorEvent.timestamp);
mCurrentIndex = (mCurrentIndex + 1) % MAX_SAMPLES;
}
示例7: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
try {
if (event.sensor == mRotationSensor && getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
update(truncatedRotationVector);
} else {
update(event.values);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
if(Math.abs(event.values[1]) < Y_DELTA_FOR_DETECT_LANDSCAPE) {
if(orientation != EventSubCodes.SAMSUNG_DEVICE_ORIENTATION_LANDSCAPE) {
Log.d(TAG, "Landscape");
}
orientation = EventSubCodes.SAMSUNG_DEVICE_ORIENTATION_LANDSCAPE;
} else {
if(orientation != EventSubCodes.SAMSUNG_DEVICE_ORIENTATION_PORTRAIT) {
Log.d(TAG, "Portrait");
}
orientation = EventSubCodes.SAMSUNG_DEVICE_ORIENTATION_PORTRAIT;
}
if(previousOrientation != orientation) {
MBApp application = MBApp.getApp();
Intent intent = new Intent(application, IPCService.class);
intent.putExtra(IPCConstants.INTENT_TYPE, EventCategories.IPC_BLE_NOTIFICATION_CHARACTERISTIC_CHANGED);
intent.putExtra(IPCConstants.INTENT_CHARACTERISTIC_MESSAGE, Utils.makeMicroBitValue
(EventCategories.SAMSUNG_DEVICE_INFO_ID, orientation));
application.startService(intent);
previousOrientation = orientation;
}
}
示例9: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
// 傳感器信息改變時執行該方法
float[] values = event.values;
float x = values[0]; // x軸方向的重力加速度,向右為正
float y = values[1]; // y軸方向的重力加速度,向前為正
float z = values[2]; // z軸方向的重力加速度,向上為正
// Log.i(TAG, "x軸方向的重力加速度" + x + ";y軸方向的重力加速度" + y + ";z軸方向的重力加速度" + z);
// 一般在這三個方向的重力加速度達到40就達到了搖晃手機的狀態。
int medumValue = 30;
if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
Log.i("Test5555", "x軸方向的重力加速度" + x + ";y軸方向的重力加速度" + y + ";z軸方向的重力加速度" + z);
vibrator.vibrate(200);
Message msg = new Message();
msg.what = SENSOR_SHAKE;
handler.sendMessage(msg);
}
}
示例10: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {//可以得到傳感器實時測量出來的變化值
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
if (x < -10) {
//direction right
} else if (x > 10) {
//direction left
if (JCVideoPlayerManager.listener() != null) {
JCVideoPlayerManager.listener().autoFullscreenLeft();
}
} else if (y > 9.5) {
if (JCVideoPlayerManager.listener() != null) {
JCVideoPlayerManager.listener().autoQuitFullscreen();
}
}
}
示例11: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
public void onSensorChanged(SensorEvent sensorEvent) {
switch (sensorEvent.sensor.getType()) {
case 1:
this.g4 = sensorEvent.values;
break;
case 2:
this.g8 = sensorEvent.values;
break;
}
if (this.g4 != null && this.g8 != null) {
float[] fArr = new float[9];
if (SensorManager.getRotationMatrix(fArr, null, this.g4, this.g8)) {
float[] fArr2 = new float[3];
SensorManager.getOrientation(fArr, fArr2);
g5 = (float) Math.toDegrees((double) fArr2[0]);
g5 = (float) Math.floor(g5 >= 0.0f ? (double) g5 : (double) (g5 + 360.0f));
}
}
}
示例12: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
// we received a sensor event. it is a good practice to check
// that we received the proper event
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, magnitudeValues, 0, magnitudeValues.length);
} else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, accelerometerValues, 0, accelerometerValues.length);
}
if (magnitudeValues != null && accelerometerValues != null) {
// Fuse accelerometer with compass
SensorManager.getRotationMatrix(currentOrientationRotationMatrix.matrix, inclinationValues, accelerometerValues,
magnitudeValues);
// Transform rotation matrix to quaternion
currentOrientationQuaternion.setRowMajor(currentOrientationRotationMatrix.matrix);
}
}
示例13: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
// we received a sensor event. it is a good practice to check
// that we received the proper event
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, magnitudeValues, 0, magnitudeValues.length);
} else if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
System.arraycopy(event.values, 0, gravityValues, 0, gravityValues.length);
}
if (magnitudeValues != null && gravityValues != null) {
// Fuse gravity-sensor (virtual sensor) with compass
SensorManager.getRotationMatrix(currentOrientationRotationMatrix.matrix, inclinationValues, gravityValues, magnitudeValues);
// Transform rotation matrix to quaternion
currentOrientationQuaternion.setRowMajor(currentOrientationRotationMatrix.matrix);
}
}
示例14: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
Float[] values = new Float[3];
values[0] = event.values[0];
updateValues(values);
//event.accuracy
//event.sensor
//event.timestamp
//Don't block the onSensorChanged() method
/*
Sensor data can change at a high rate, which means the system may call the
onSensorChanged(SensorEvent) method quite often. As a best practice, you
should do as little as possible within the onSensorChanged(SensorEvent) method
so you don't block it. If your application requires you to do any data filtering
or reduction of sensor data, you should perform that work outside of the
onSensorChanged(SensorEvent) method.
*/
}
示例15: onSensorChanged
import android.hardware.SensorEvent; //導入依賴的package包/類
@Override
public void onSensorChanged(SensorEvent event) {
boolean accelerating = isAccelerating(event);
long timestamp = event.timestamp;
queue.add(timestamp, accelerating);
if (queue.isShaking()) {
/*
* detect time between two concecutive shakes and limit it to
* MIN_TIME_BETWEEN_TWO_SHAKES
*/
long currentTime = System.currentTimeMillis();
if (currentTime - mDetectedShakeStartTime > MIN_TIME_BETWEEN_TWO_SHAKES) {
queue.clear();
listener.hearShake();
mDetectedShakeStartTime = System.currentTimeMillis();
}
}
}