欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现摇一摇功能

程序员文章站 2024-03-01 19:46:04
实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下: 1、在 androidmanifest.xml 中添加操作权限 2、实现代码...

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

1、在 androidmanifest.xml 中添加操作权限

2、实现代码

 package com.xs.test; 
 
import android.app.activity; 
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.toast; 
 
/** 
 * 安卓晃动手机监听--“摇一摇” 
 * 
 * @author 单红宇 
 * 
 */ 
public class testsensoractivity extends activity { 
 
  private sensormanager sensormanager; 
  private vibrator vibrator; 
 
  private static final string tag = "testsensoractivity"; 
  private static final int sensor_shake = 10; 
 
  /** called when the activity is first created. */ 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    sensormanager = (sensormanager) getsystemservice(sensor_service); 
    vibrator = (vibrator) getsystemservice(vibrator_service); 
  } 
 
  @override 
  protected void onresume() { 
    super.onresume(); 
    if (sensormanager != null) {// 注册监听器 
      sensormanager.registerlistener(sensoreventlistener, sensormanager.getdefaultsensor(sensor.type_accelerometer), sensormanager.sensor_delay_normal); 
      // 第一个参数是listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率 
    } 
  } 
 
  @override 
  protected void onpause() { 
    super.onpause(); 
    if (sensormanager != null) {// 取消监听器 
      sensormanager.unregisterlistener(sensoreventlistener); 
    } 
  } 
 
  /** 
   * 重力感应监听 
   */ 
  private sensoreventlistener sensoreventlistener = new sensoreventlistener() { 
 
    @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 = 19;// 三星 i9250怎么晃都不会超过20,没办法,只设置19了 
      if (math.abs(x) > medumvalue || math.abs(y) > medumvalue || math.abs(z) > medumvalue) { 
        vibrator.vibrate(200); 
        message msg = new message(); 
        msg.what = sensor_shake; 
        handler.sendmessage(msg); 
      } 
    } 
 
    @override 
    public void onaccuracychanged(sensor sensor, int accuracy) { 
 
    } 
  }; 
 
  /** 
   * 动作执行 
   */ 
  handler handler = new handler() { 
 
    @override 
    public void handlemessage(message msg) { 
      super.handlemessage(msg); 
      switch (msg.what) { 
      case sensor_shake: 
        toast.maketext(testsensoractivity.this, "检测到摇晃,执行操作!", toast.length_short).show(); 
        log.i(tag, "检测到摇晃,执行操作!"); 
        break; 
      } 
    } 
 
  }; 
} 

这种摇一摇比较普通,没有经过算法的加强,重力加速度就是10。
算法优化如下

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) {

}
};

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。