Android实现摇一摇功能
实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下:
1、在AndroidManifest.xml中添加操作权限
2、实现代码
packagecom.xs.test;
importandroid.app.Activity;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.os.Vibrator;
importandroid.util.Log;
importandroid.widget.Toast;
/**
*安卓晃动手机监听--“摇一摇”
*
*@author单红宇
*
*/
publicclassTestSensorActivityextendsActivity{
privateSensorManagersensorManager;
privateVibratorvibrator;
privatestaticfinalStringTAG="TestSensorActivity";
privatestaticfinalintSENSOR_SHAKE=10;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
vibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE);
}
@Override
protectedvoidonResume(){
super.onResume();
if(sensorManager!=null){//注册监听器
sensorManager.registerListener(sensorEventListener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
//第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率
}
}
@Override
protectedvoidonPause(){
super.onPause();
if(sensorManager!=null){//取消监听器
sensorManager.unregisterListener(sensorEventListener);
}
}
/**
*重力感应监听
*/
privateSensorEventListenersensorEventListener=newSensorEventListener(){
@Override
publicvoidonSensorChanged(SensorEventevent){
//传感器信息改变时执行该方法
float[]values=event.values;
floatx=values[0];//x轴方向的重力加速度,向右为正
floaty=values[1];//y轴方向的重力加速度,向前为正
floatz=values[2];//z轴方向的重力加速度,向上为正
Log.i(TAG,"x轴方向的重力加速度"+x+";y轴方向的重力加速度"+y+";z轴方向的重力加速度"+z);
//一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。
intmedumValue=19;//三星i9250怎么晃都不会超过20,没办法,只设置19了
if(Math.abs(x)>medumValue||Math.abs(y)>medumValue||Math.abs(z)>medumValue){
vibrator.vibrate(200);
Messagemsg=newMessage();
msg.what=SENSOR_SHAKE;
handler.sendMessage(msg);
}
}
@Override
publicvoidonAccuracyChanged(Sensorsensor,intaccuracy){
}
};
/**
*动作执行
*/
Handlerhandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
switch(msg.what){
caseSENSOR_SHAKE:
Toast.makeText(TestSensorActivity.this,"检测到摇晃,执行操作!",Toast.LENGTH_SHORT).show();
Log.i(TAG,"检测到摇晃,执行操作!");
break;
}
}
};
}
这种摇一摇比较普通,没有经过算法的加强,重力加速度就是10。
算法优化如下
packagecom.example.url;
importjava.io.IOException;
importjava.net.URL;
importcom.hahashijie.imageload.ImageLoader;
importandroid.annotation.SuppressLint;
importandroid.app.Activity;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.drawable.Drawable;
importandroid.hardware.Sensor;
importandroid.hardware.SensorEvent;
importandroid.hardware.SensorEventListener;
importandroid.hardware.SensorManager;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.os.Vibrator;
importandroid.util.Log;
importandroid.widget.ImageView;
@SuppressLint("HandlerLeak")
publicclassMainActivityextendsActivity{
privateImageViewimage;
privateSensorManagersensorManager;
privateSensorsensor;
privateVibratorvibrator;
privatestaticfinalintUPTATE_INTERVAL_TIME=50;
privatestaticfinalintSPEED_SHRESHOLD=30;//这个值调节灵敏度
privatelonglastUpdateTime;
privatefloatlastX;
privatefloatlastY;
privatefloatlastZ;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
vibrator=(Vibrator)getSystemService(VIBRATOR_SERVICE);
image=(ImageView)findViewById(R.id.image);
}
@Override
protectedvoidonResume(){
//TODOAuto-generatedmethodstub
super.onResume();
if(sensorManager!=null){
sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
if(sensor!=null){
sensorManager.registerListener(sensorEventListener,
sensor,
SensorManager.SENSOR_DELAY_GAME);//这里选择感应频率
}
}
/**
*重力感应监听
*/
privateSensorEventListenersensorEventListener=newSensorEventListener(){
@Override
publicvoidonSensorChanged(SensorEventevent){
longcurrentUpdateTime=System.currentTimeMillis();
longtimeInterval=currentUpdateTime-lastUpdateTime;
if(timeInterval<UPTATE_INTERVAL_TIME){
return;
}
lastUpdateTime=currentUpdateTime;
//传感器信息改变时执行该方法
float[]values=event.values;
floatx=values[0];//x轴方向的重力加速度,向右为正
floaty=values[1];//y轴方向的重力加速度,向前为正
floatz=values[2];//z轴方向的重力加速度,向上为正
floatdeltaX=x-lastX;
floatdeltaY=y-lastY;
floatdeltaZ=z-lastZ;
lastX=x;
lastY=y;
lastZ=z;
doublespeed=(Math.sqrt(deltaX*deltaX+deltaY*deltaY
+deltaZ*deltaZ)/timeInterval)*100;
if(speed>=SPEED_SHRESHOLD){
vibrator.vibrate(300);
image.setImageResource(R.drawable.running01);
}
}
@Override
publicvoidonAccuracyChanged(Sensorsensor,intaccuracy){
}
};
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。