當前位置: 首頁>>代碼示例>>Java>>正文


Java SensorEvent類代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:googlecodelabs,項目名稱:androidthings-weatherstation,代碼行數:11,代碼來源:WeatherStationActivity.java

示例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);
            }
        }
 
開發者ID:Datatellit,項目名稱:xlight_android_native,代碼行數:24,代碼來源:SlidingMenuMainActivity.java

示例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;
//        }
	}
 
開發者ID:StringMon,項目名稱:homescreenarcade,代碼行數:25,代碼來源:SpaceInvadersActivity.java

示例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;
}
 
開發者ID:qq565999484,項目名稱:RNLearn_Project1,代碼行數:22,代碼來源:ShakeDetector.java

示例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();
    }
}
 
開發者ID:entria,項目名稱:react-native-camera-face-detector,代碼行數:19,代碼來源:RCTSensorOrientationChecker.java

示例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;
}
 
開發者ID:aesean,項目名稱:ActivityStack,代碼行數:19,代碼來源:ShakeDetector.java

示例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();
	}
}
 
開發者ID:SebastianRask,項目名稱:Pocket-Plays-for-Twitch,代碼行數:17,代碼來源:StreamActivity.java

示例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;
    }
}
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:27,代碼來源:OrientationChangedPresenter.java

示例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);
    }
}
 
開發者ID:fergus825,項目名稱:SmartOrnament,代碼行數:19,代碼來源:ShakeService.java

示例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();
        }
    }

}
 
開發者ID:wp521,項目名稱:MyFire,代碼行數:20,代碼來源:JCVideoPlayer.java

示例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));
        }
    }
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:20,代碼來源:af.java

示例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);
    }
}
 
開發者ID:peter10110,項目名稱:Android-SteamVR-controller,代碼行數:20,代碼來源:AccelerometerCompassProvider.java

示例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);
    }
}
 
開發者ID:peter10110,項目名稱:Android-SteamVR-controller,代碼行數:19,代碼來源:GravityCompassProvider.java

示例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.
    */
}
 
開發者ID:if710,項目名稱:2017.2-codigo,代碼行數:20,代碼來源:SensorSingleValueActivity.java

示例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();
        }
    }
}
 
開發者ID:komamj,項目名稱:KomaMusic,代碼行數:19,代碼來源:ShakeDetector.java


注:本文中的android.hardware.SensorEvent類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。