onSensorChanged(SensorEvent event)

这个目的是检测 当手机被晃动后 单位时间里用急快的周期检测, 最后取出最大值!最后再输出最大值给另一个gameView对象 创建下一个场景!
@Override
	public void onSensorChanged(SensorEvent event) {

		curTime = System.currentTimeMillis();
		float z = event.values[2];
		float y = event.values[1];

		if ((curTime - lastUpdate) > 5) {
			long diffTime = (curTime - lastUpdate);
			lastUpdate = curTime;
			if ( Math.abs(z - lastZ) > 1 && Math.abs(y - lastY) > 1/2){ 
				
				if (counter <= 19 && flag){
					getZ [counter] = Math.abs(z);
					getY [counter] = Math.abs(y);
					Log.e(tag, "counter: = " + counter + "--->" + getZ[counter]);
					Log.e(tag, "counter: = " + counter + "--->" + getY[counter]);
					counter ++;
					if(counter == 19){

						Arrays.sort(getZ);
						Arrays.sort(getY);
						
						maxZ = getZ[19];
						maxY = getY[19];
						Log.e(tag, "" + maxZ + "" + maxY);
						if (maxZ + maxY > 40){
							max = 10;
						}
						if (maxZ + maxY <= 40){
							max = (int) ((maxZ + maxY) / 4);
						}
						
						Log.e(tag, "" + max);
						gameView = new GameView(this, max);
						setContentView(gameView);
						flag = false;
					}
				}
			}
		}
		lastZ = z;
		lastY = y;
		
	} // 回头再解释 这个代码是干啥的吧 最近忙!

你可能感兴趣的:(android,event,ACCELEROMETER,SensorEvent,onSensorChanged)