实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下: 1、在 AndroidManifest.xml 中添加操作权限 2、实现代码 package
实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下:
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;
public class TestSensorActivity extends Activity {
private SensorManager sensorManager;
private Vibrator vibrator;
private static final String TAG = "TestSensorActivity";
private static final int SENSOR_SHAKE = 10;
@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) {
}
};
}
您可能感兴趣的文章:android重力感应开发之微信摇一摇功能Android编程使用加速度传感器实现摇一摇功能及优化的方法详解Android实现iPhone晃动撤销输入功能 Android仿微信摇一摇功能Android利用传感器仿微信摇一摇功能Android利用传感器实现微信摇一摇功能Android 微信摇一摇功能实现详细介绍android 类似微信的摇一摇功能实现思路及代码java仿微信摇一摇实现播放音乐
--结束END--
本文标题: Android实现摇一摇功能
本文链接: https://lsjlt.com/news/23363.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0