今天看啥
热点:

android 实现摇一摇功能


实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下:

一、在 AndroidManifest.xml 中添加操作权限

二、实现代码

 

[java] 
  1. package com.xs.test;
  2.  
  3. import android.app.Activity;
  4. import android.hardware.Sensor;
  5. import android.hardware.SensorEvent;
  6. import android.hardware.SensorEventListener;
  7. import android.hardware.SensorManager;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.os.Vibrator;
  12. import android.util.Log;
  13. import android.widget.Toast;
  14.  
  15. /**
  16. * 安卓晃动手机监听--“摇一摇”
  17. *
  18. * @author 单红宇
  19. *
  20. */
  21. public class TestSensorActivity extends Activity {
  22.  
  23. private SensorManager sensorManager;
  24. private Vibrator vibrator;
  25.  
  26. private static final String TAG = TestSensorActivity;
  27. private static final int SENSOR_SHAKE = 10;
  28.  
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  35. vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
  36. }
  37.  
  38. @Override
  39. protected void onResume() {
  40. super.onResume();
  41. if (sensorManager != null) {// 注册监听器
  42. sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
  43. // 第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率
  44. }
  45. }
  46.  
  47. @Override
  48. protected void onPause() {
  49. super.onPause();
  50. if (sensorManager != null) {// 取消监听器
  51. sensorManager.unregisterListener(sensorEventListener);
  52. }
  53. }
  54.  
  55. /**
  56. * 重力感应监听
  57. */
  58. private SensorEventListener sensorEventListener = new SensorEventListener() {
  59.  
  60. @Override
  61. public void onSensorChanged(SensorEvent event) {
  62. // 传感器信息改变时执行该方法
  63. float[] values = event.values;
  64. float x = values[0]; // x轴方向的重力加速度,向右为正
  65. float y = values[1]; // y轴方向的重力加速度,向前为正
  66. float z = values[2]; // z轴方向的重力加速度,向上为正
  67. Log.i(TAG, x轴方向的重力加速度 + x + ;y轴方向的重力加速度 + y + ;z轴方向的重力加速度 + z);
  68. // 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。
  69. int medumValue = 19;// 三星 i9250怎么晃都不会超过20,没办法,只设置19了
  70. if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
  71. vibrator.vibrate(200);
  72. Message msg = new Message();
  73. msg.what = SENSOR_SHAKE;
  74. handler.sendMessage(msg);
  75. }
  76. }
  77.  
  78. @Override
  79. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  80.  
  81. }
  82. };
  83.  
  84. /**
  85. * 动作执行
  86. */
  87. Handler handler = new Handler() {
  88.  
  89. @Override
  90. public void handleMessage(Message msg) {
  91. super.handleMessage(msg);
  92. switch (msg.what) {
  93. case SENSOR_SHAKE:
  94. Toast.makeText(TestSensorActivity.this, 检测到摇晃,执行操作!, Toast.LENGTH_SHORT).show();
  95. Log.i(TAG, 检测到摇晃,执行操作!);
  96. break;
  97. }
  98. }
  99.  
  100. };
  101. }
  102. 这种摇一摇比较普通,没有经过算法的加强,重力加速度就是10。
  103. 算法优化如下
  104. package com.example.url;


    import java.io.IOException;
    import java.net.URL;


    import com.hahashijie.imageload.ImageLoader;


    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.Drawable;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.os.Vibrator;
    import android.util.Log;
    import android.widget.ImageView;


    @SuppressLint(HandlerLeak)
    public class MainActivity extends Activity {


    private ImageView image;
    private SensorManager sensorManager;
    private Sensor sensor;
    private Vibrator vibrator;
    private static final int UPTATE_INTERVAL_TIME = 50;
    private static final int SPEED_SHRESHOLD = 30;//这个值调节灵敏度
    private long lastUpdateTime;
    private float lastX;
    private float lastY;
    private float lastZ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    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
    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if (sensorManager != null) {
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }
    if (sensor != null) {
    sensorManager.registerListener(sensorEventListener,
    sensor,
    SensorManager.SENSOR_DELAY_GAME);//这里选择感应频率
    }
    }


    /**
    * 重力感应监听
    */
    private SensorEventListener sensorEventListener = new SensorEventListener() {


    @Override
    public void onSensorChanged(SensorEvent event) {
    long currentUpdateTime = System.currentTimeMillis();
    long timeInterval = currentUpdateTime - lastUpdateTime;
    if (timeInterval < UPTATE_INTERVAL_TIME) {
    return;
    }
    lastUpdateTime = currentUpdateTime;
    // 传感器信息改变时执行该方法
    float[] values = event.values;
    float x = values[0]; // x轴方向的重力加速度,向右为正
    float y = values[1]; // y轴方向的重力加速度,向前为正
    float z = values[2]; // 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) * 100;
    if (speed >= SPEED_SHRESHOLD) {
    vibrator.vibrate(300);
    image.setImageResource(R.drawable.running01);
    }
    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
    };

    }

www.bkjia.comtruehttp://www.bkjia.com/Androidjc/1004085.htmlTechArticleandroid 实现摇一摇功能 实现摇一摇功能,其实很简单,就是检测手机的重力感应,具体实现代码如下: 一、在 AndroidManifest.xml 中添加操作权...

相关文章

相关搜索: 功能

帮客评论

视觉看点