Java Code Examples for android.hardware.SensorManager#DATA_Z

The following examples show how to use android.hardware.SensorManager#DATA_Z . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShakeDetectActivity.java    From ListView-Swipe-to-Delete with Apache License 2.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
	if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER) {

		long curTime = System.currentTimeMillis();
		// if a shake in last X seconds ignore.
		if (lastShake != 0 && (curTime - lastShake) < IGNORE_EVENTS_AFTER_SHAKE) return;

		float x = event.values[SensorManager.DATA_X];
		float y = event.values[SensorManager.DATA_Y];
		float z = event.values[SensorManager.DATA_Z];
		if (last_x != 0 && last_y != 0 && last_z != 0 && (last_x != x || last_y != y || last_z != z)) {
			DataPoint dp = new DataPoint(last_x-x, last_y-y, last_z-z, curTime);
			//Log.i("XYZ",Float.toString(dp.x)+"   "+Float.toString(dp.y)+"   "+Float.toString(dp.z)+"   ");
			dataPoints.add(dp);

			if ((curTime - lastUpdate) > SHAKE_CHECK_THRESHOLD) {
				lastUpdate = curTime;
				checkForShake();
			}
		}
		last_x = x;
		last_y = y;
		last_z = z;
	}		
}
 
Example 2
Source File: ShakeDetectEventListener.java    From android-wear-gopro-remote with Apache License 2.0 6 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        long curTime = System.currentTimeMillis();
        // if a shake in last X seconds ignore.
        if (lastShake != 0 && (curTime - lastShake) < IGNORE_EVENTS_AFTER_SHAKE) return;

        float x = event.values[SensorManager.DATA_X];
        float y = event.values[SensorManager.DATA_Y];
        float z = event.values[SensorManager.DATA_Z];
        if (last_x != 0 && last_y != 0 && last_z != 0 && (last_x != x || last_y != y || last_z != z)) {
            DataPoint dp = new DataPoint(last_x-x, last_y-y, last_z-z, curTime);
            //Log.i("XYZ",Float.toString(dp.x)+"   "+Float.toString(dp.y)+"   "+Float.toString(dp.z)+"   ");
            dataPoints.add(dp);

            if ((curTime - lastUpdate) > SHAKE_CHECK_THRESHOLD) {
                lastUpdate = curTime;
                checkForShake();
            }
        }
        last_x = x;
        last_y = y;
        last_z = z;
    }
}
 
Example 3
Source File: JCVideoPlayer.java    From JCVideoPlayer with MIT License 6 votes vote down vote up
@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 < -11) {
        //direction right
    } else if (x > 11) {
        //direction left
        if (JCVideoPlayerManager.listener() != null) {
            JCVideoPlayerManager.listener().autoFullscreenLeft();
        }
    } else if (y > 11) {
        if (JCVideoPlayerManager.listener() != null) {
            JCVideoPlayerManager.listener().autoQuitFullscreen();
        }
    }

}
 
Example 4
Source File: JZVideoPlayer.java    From JZVideoDemo with MIT License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {//可以得到传感器实时测量出来的变化值
    final float x = event.values[SensorManager.DATA_X];
    float y = event.values[SensorManager.DATA_Y];
    float z = event.values[SensorManager.DATA_Z];
    //过滤掉用力过猛会有一个反向的大数值
    if (x < -12 || x > 12) {
        if ((System.currentTimeMillis() - lastAutoFullscreenTime) > 2000) {
            if (JZVideoPlayerManager.getCurrentJzvd() != null) {
                JZVideoPlayerManager.getCurrentJzvd().autoFullscreen(x);
            }
            lastAutoFullscreenTime = System.currentTimeMillis();
        }
    }
}
 
Example 5
Source File: ShakeUpdateListener.java    From YiBo with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
	long currentTime = System.currentTimeMillis();
    if (currentTime - lastShakeTime < SHAKE_INTERVAL_TIME 
    	|| !sheJiaoMao.isRefreshOnShake()) {
    	return;
    }
 
    //获取加速度传感器的三个参数
    float x = event.values[SensorManager.DATA_X];
    float y = event.values[SensorManager.DATA_Y];
    float z = event.values[SensorManager.DATA_Z];
    //System.out.println("加速度: x->" + x + ", y->" + y + ", z->" + z);

	float tempDiffX = x - lastX;
	float tempDiffY = y - lastY;
    lastX = x;
    lastY = y;
    if (lastDiffX < 0 
    	&& lastDiffY < 0 
    	&& tempDiffX > SHAKE_UPDATE_ACCELERATION_X 
    	&& tempDiffY > SHAKE_UPDATE_ACCELERATION_Y) {
    	if (Logger.isDebug()) Log.v(TAG, "vibrateToUpdate: x->" + x + ", y->" + y + ", z->" + z);
    	vibrateToUpdate();
    }
    
    lastDiffX = tempDiffX;
    lastDiffY = tempDiffY;
}
 
Example 6
Source File: DragableView.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    long curTime = System.currentTimeMillis();
    // 每100毫秒检测一次
    if ((curTime - lastUpdate) > 100) {
        long diffTime = (curTime - lastUpdate);
        lastUpdate = curTime;
        x = event.values[SensorManager.DATA_X];
        y = event.values[SensorManager.DATA_Y];
        z = event.values[SensorManager.DATA_Z];
        float speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
        if (speed > SHAKE_THRESHOLD) {
            // 检测到摇晃后执行的代码
            setVisibility(View.GONE);
            setClickable(false);
            sensorManager.unregisterListener(this, sensor);
            if (!isAddedSenseView) {
                windowManager.addView(gSenseView, gSenseView.getLayoutParams());
                isAddedSenseView = true;
                gSenseView.startSense();
                gSenseView.addBallFallIntoCallback(new OnBallFallIntoCallback() {
                    @Override
                    public void onFallInto() {
                        windowManager.removeViewImmediate(gSenseView);
                        isAddedSenseView = false;
                        if (listener != null) {
                            listener.onGSBallFalled();
                        }
                    }
                });

            }

        }
        last_x = x;
        last_y = y;
        last_z = z;
    }
}
 
Example 7
Source File: ShakeMonitor.java    From RoMote with Apache License 2.0 5 votes vote down vote up
public final void onSensorChanged(SensorEvent event) {
    // The light sensor returns a single value.
    // Many sensors return 3 values, one for each axis.
    float [] values = event.values;
    // Do something with this sensor value.

    long now = System.currentTimeMillis();

    if ((now - mLastForce) > SHAKE_TIMEOUT) {
        mShakeCount = 0;
    }

    if ((now - mLastTime) > TIME_THRESHOLD) {
        long diff = now - mLastTime;
        float speed = Math.abs(values[SensorManager.DATA_X] + values[SensorManager.DATA_Y] + values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
        if (speed > FORCE_THRESHOLD) {
            if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
                mLastShake = now;
                mShakeCount = 0;
                if (mShakeListener != null) {
                    mShakeListener.onShake();
                }
            }
            mLastForce = now;
        }
        mLastTime = now;
        mLastX = values[SensorManager.DATA_X];
        mLastY = values[SensorManager.DATA_Y];
        mLastZ = values[SensorManager.DATA_Z];
    }
}
 
Example 8
Source File: ShakeListener.java    From iMoney with Apache License 2.0 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {
    Log.i("TAG", "x:" + event.values[SensorManager.DATA_X] + "  y:" +
            event.values[SensorManager.DATA_Y] + "  z:" +
            event.values[SensorManager.DATA_Z]);

    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
        return;
    }

    long now = System.currentTimeMillis();

    if ((now - mLastForce) > SHAKE_TIMEOUT) {
        mShakeCount = 0;
    }

    if ((now - mLastTime) > TIME_THRESHOLD) {
        long diff = now - mLastTime;
        // 把X,Y,Z方向的距离除以时间,得出速度
        float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
        if (speed > FORCE_THRESHOLD) {//如果速度大于某个值
            // 先把摇晃的次数+1,再判断是否超过了要换的次数,并且间隙大于特定的值
            if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
                mLastShake = now;
                mShakeCount = 0;
                if (mShakeListener != null) {//回调我们的listener
                    mShakeListener.onShake();
                }
            }
            mLastForce = now;
        }
        mLastTime = now;
        mLastX = event.values[SensorManager.DATA_X];
        mLastY = event.values[SensorManager.DATA_Y];
        mLastZ = event.values[SensorManager.DATA_Z];
    }
}
 
Example 9
Source File: RestProtector.java    From Huochexing12306 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void onSensorChanged(SensorEvent event) {
	if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){
		return;
	}
           //现在检测时间   
            long currentUpdateTime = System.currentTimeMillis();   
            //两次检测的时间间隔   
            long timeInterval = currentUpdateTime - lastUpdateTime;     
            //判断是否达到了检测时间间隔   
            if(timeInterval < UPTATE_INTERVAL_TIME)    
             return;   
            //现在的时间变成last时间   
            lastUpdateTime = currentUpdateTime;   
          //获取加速度数值,以下三个值为重力分量在设备坐标的分量大小  
           float x = event.values[SensorManager.DATA_X];            
           float y = event.values[SensorManager.DATA_Y];            
           float z = event.values[SensorManager.DATA_Z];
            //获得x,y,z的变化值   
            float deltaX = x - lastX;   
            float deltaY = y - lastY;
            float deltaZ = z - lastZ;      
            //备份本次坐标  
            lastX = x;   
            lastY = y;   
            lastZ = z;     
            //计算移动速度  
            double speed = Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ)/timeInterval * 10000;  
            L.i("speed:" + speed);
            L.i("mSpeedThreshold:" + mSpeedThreshold);   
            if(speed >= mSpeedThreshold){
               request(AntiTheftService.REQUEST_START_ALARM, null);
            }
}
 
Example 10
Source File: Jzvd.java    From imsdk-android with MIT License 5 votes vote down vote up
@Override
public void onSensorChanged(SensorEvent event) {//可以得到传感器实时测量出来的变化值
    final float x = event.values[SensorManager.DATA_X];
    float y = event.values[SensorManager.DATA_Y];
    float z = event.values[SensorManager.DATA_Z];
    //过滤掉用力过猛会有一个反向的大数值
    if (x < -12 || x > 12) {
        if ((System.currentTimeMillis() - lastAutoFullscreenTime) > 2000) {
            if (Jzvd.CURRENT_JZVD != null) Jzvd.CURRENT_JZVD.autoFullscreen(x);
            lastAutoFullscreenTime = System.currentTimeMillis();
        }
    }
}
 
Example 11
Source File: AccelerationData.java    From tilt-game-android with MIT License 4 votes vote down vote up
public float getZ() {
	return this.mValues[SensorManager.DATA_Z];
}
 
Example 12
Source File: AccelerationData.java    From tilt-game-android with MIT License 4 votes vote down vote up
public void setZ(final float pZ) {
	this.mValues[SensorManager.DATA_Z] = pZ;
}
 
Example 13
Source File: OrientationData.java    From tilt-game-android with MIT License 4 votes vote down vote up
public float getRoll() {
	return super.mValues[SensorManager.DATA_Z];
}
 
Example 14
Source File: AccelerationData.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
public float getZ() {
	return this.mValues[SensorManager.DATA_Z];
}
 
Example 15
Source File: AccelerationData.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
public void setZ(final float pZ) {
	this.mValues[SensorManager.DATA_Z]  = pZ;
}
 
Example 16
Source File: OrientationData.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
public float getRoll() {
	return super.mValues[SensorManager.DATA_Z];
}
 
Example 17
Source File: Orientation.java    From WhereYouGo with GNU General Public License v3.0 4 votes vote down vote up
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
  case Sensor.TYPE_MAGNETIC_FIELD:
    break;
  case Sensor.TYPE_ACCELEROMETER:
    float filter = getFilter();
    aboveOrBelow =
            (float) ((event.values[SensorManager.DATA_Z] * filter) + (aboveOrBelow * (1.0 - filter)));
    break;
  case Sensor.TYPE_ORIENTATION:
    float valueOr = event.values[SensorManager.DATA_X];
    // Logger.d(TAG, "sensorOrientation:" + valueOr + ", " + event.values[SensorManager.DATA_Y]
    // + ", " + event.values[SensorManager.DATA_Z] + ", " + getDeclination());
    // fix to true bearing
    if (Preferences.SENSOR_BEARING_TRUE) {
      valueOr += getDeclination();
    }
    orient = filterValue(valueOr, orient);
    pitch = filterValue(event.values[SensorManager.DATA_Y], pitch);

    roll = filterValue(event.values[SensorManager.DATA_Z], roll);
    float rollDef;
    if (aboveOrBelow < 0) {
      if (roll < 0) {
        rollDef = -180 - roll;
      } else {
        rollDef = 180 - roll;
      }
    } else {
      rollDef = roll;
    }
    this.mLastAziSensor = orient;

    // do some orientation change by settings
    int rotation = A.getMain().getWindowManager().getDefaultDisplay().getRotation();
    switch (rotation) {
      case Surface.ROTATION_0:
        // no need for change
        break;
      case Surface.ROTATION_90:
        mLastAziSensor += 90;
        break;
      case Surface.ROTATION_180:
        mLastAziSensor -= 180;
        break;
      case Surface.ROTATION_270:
        mLastAziSensor -= 90;
        break;
    }

    sendOrientation(pitch, rollDef);
    break;
}
}