Android的重力传感器(3轴加速度传感器)简单实例
重力感应主要是依靠手机的加速度传感器(accelerometer)来实现
在Android的开发中一共有八种传感器但是不一定每一款真机都支持这些传感器。因为很多功能用户根本不care的所以可能开发商会把某些功能屏蔽掉。还是得根据真机的实际情况来做开发,今天我们主要来讨论加速度传感器的具体实现方式。
传感器名称如下:
加速度传感器(accelerometer)
陀螺仪传感器(gyroscope)
环境光照传感器(light)
磁力传感器(magnetic field)
方向传感器(orientation)
压力传感器(pressure)
距离传感器(proximity)
温度传感器(temperature)
上面的是程序的运行图
遇到的问题:
1、当在与球相同的布局里面调用TextView时,球就不能移动了。最后我把球和数据分离开,用两个布局处理的。
2、不明白super.setFrame()函数到底是什么意思
有哪位大神看懂了,告诉我一下
这个是主程序代码:
package cn.itcast.accelerometer; import cn.itcast.accelerometer.view.BallView;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* 作者:itas109
* 博客:http://blog.csdn.net/itas109
* 欢迎大家到博客交流
*
* 原作者信息------>
* 利用重力加速度传感器实现滚桌球游戏
* 参考资料:
* 1> http://ophonesdn.com/article/show/183
* 2> 重力加速度原理学习资料 http://www.riameeting.com/node/538
* 由于重力加速度传感器需要在真机才能测试,为了能在模拟器中测试,可以按下面文章搭建测试环境:
* http://www.xiaojiayi.com/
*
*/
public class AccelerometerActivity extends Activity {
private static final float MAX_ACCELEROMETER = 9.81f;
private SensorManager sensorManager;
private BallView ball;
private boolean success = false;
private boolean init = false;
private int container_width = 0;
private int container_height = 0;
private int ball_width = 0;
private int ball_height = 0;
private TextView prompt;
private TextView tv1;
private TextView tv2;
private TextView tv3;
/*
private int a=0;
private int b=0;
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取感应器管理器
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
prompt = (TextView) findViewById(R.id.ball_prompt);
tv1 = (TextView)findViewById(R.id.tv1);
tv2 = (TextView)findViewById(R.id.tv2);
tv3 = (TextView)findViewById(R.id.tv3);
} @Override
public void onWindowFocusChanged(boolean hasFocus) {//ball_container控件显示出来后才能获取其宽和高,所以在此方法得到其宽高
super.onWindowFocusChanged(hasFocus);
if(hasFocus && !init){
View container = findViewById(R.id.ball_container);
container_width = container.getWidth();
container_height = container.getHeight();
ball = (BallView) findViewById(R.id.ball);
ball_width = ball.getWidth();
ball_height = ball.getHeight();
moveTo(0f, 0f);
init = true;
}
} @Override
protected void onResume() {
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//获取重力加速度感应器
success = sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);//注册listener,第三个参数是检测的精确度
super.onResume();
} @Override
protected void onPause() {
if(success) sensorManager.unregisterListener(listener);
super.onPause();
} private SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) { /*
Button bt1= (Button)findViewById(R.id.bt1);//测试图片移动
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View paramView) {
moveTo(0,++b);
//if(b>50)
// b=0;
}
}); Button bt2= (Button)findViewById(R.id.bt2);
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View paramView) {
moveTo(0,--b);
//if(b>50)
// b=0;
}
});
*/ if (!init) return ;
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
prompt.setText("X=" + x + ",Y= " + y + ", Z=" + z);
//当重力x,y为0时,球处于中心位置,以y为轴心(固定不动),转动手机,x会在(0-9.81)之间变化,负号代表方向
moveTo(-x, y);//x方向取反 if(x>0){
tv1.setTextColor(Color.WHITE);
tv1.setText("向左");
} if(x<0){
tv1.setTextColor(Color.CYAN);
tv1.setText("向右");
} if(y>0){
tv2.setTextColor(Color.WHITE);
tv2.setText("向后");
} if(y<0){
tv2.setTextColor(Color.RED);
tv2.setText("向前");
} if(z>0){
tv3.setTextColor(Color.WHITE);
tv3.setText("向上");
} if(z<0){
tv3.setTextColor(Color.YELLOW);
tv3.setText("向下");
} }
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}; private void moveTo(float x, float y) { int max_x = (container_width - ball_width) / 2;//在x轴可移动的最大值
int max_y = (container_height - ball_height) / 2;//在y轴可移动的最大值
//手机沿x、y轴垂直摆放时,自由落体加速度最大为9.81,当手机沿x、y轴成某个角度摆放时,变量x和y即为该角度的加速度
float percentageX = x / MAX_ACCELEROMETER;//得到当前加速度的比率,如果手机沿x轴垂直摆放,比率为100%,即球在x轴上移动到最大值
float percentageY = y / MAX_ACCELEROMETER; int pixel_x = (int) (max_x * percentageX);//得到x轴偏移量
int pixel_y = (int) (max_y * percentageY);//得到y轴偏移量
//以球在中心位置的坐标为参考点,加上偏移量,得到球的对应位置,然后移动球到该位置 int x3=max_x + pixel_x;//屏幕中心位置+x轴偏移
int y3=max_y + pixel_y;//屏幕中心位置+y轴偏移 ball.moveTo(x3, y3); }
}
球的代码移动:
package cn.itcast.accelerometer.view; import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView; public class BallView extends ImageView { public BallView(Context context) {
super(context);
} public BallView(Context context, AttributeSet attrs) {
super(context, attrs);
} public BallView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public void moveTo(int x, int y) {//没有弄明白什么意思,谁懂了告诉我一下啊,我加一个TextView,球就不能移动了 itas109
super.setFrame(x, y, x + getWidth(), y + getHeight());//绘制视图,由左上角与右下角确定视图矩形位置
}
}
整个程序的下载地址:
http://download.csdn.net/detail/itas109/6000447
Android的重力传感器(3轴加速度传感器)简单实例的更多相关文章
- 六轴加速度传感器MPU6050官方DMP库到瑞萨RL78/G13的移植
2015年的电赛已经结束了.赛前接到器件清单的时候,看到带防护圈的多旋翼飞行器赫然在列,又给了一个瑞萨RL78/G13的MCU,于是自然联想到13年的电赛,觉得多半是拿RL78/G13做四旋翼的主控, ...
- 【传感器】BMA253 数字,三轴加速度传感器
参考文档:BMA253E DataSheet 参考文档链接 密码:9new BMA253 数字,三轴加速度传感器 关键特性: 关键特性 封装方式 LGA封装(12pins),长*宽(2mm*2mm ...
- Android安卓下拉阻尼效果实现原理及简单实例
原理 这种效果是通过自定义控件的方式来实现的,我自定义了一个控件类型,这个自定义控件(PullDownDumperLayout)继承自线性布局(LinearLayout). 用户可以下拉弹出的那个 ...
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
由于andorid不推荐用传统的方向传感器,推荐用加速度传感器和地磁传感器来构造得到方向传感器的数据,其实主要是z轴的旋转角度 具体代码示例 代码如下 public class MainActivit ...
- OneNET麒麟座应用开发之五:获取加速度传感器ADXL345数据
由于数据采集站基本都安装在野外或者楼顶,安装位置以及震动对检测数据的准确性有一定影响.所以想要有一个位置状态数据,正好发现麒麟作上有ADXL345,这样一个数字输出的加速度传感器.如图中红框所示: 1 ...
- ADXL3xx: 读取 ADXL3xx 加速度传感器
原文链接:https://www.arduino.cc/en/Tutorial/ADXL3xx ADXL3xx加速度传感器 本教程将为你展示如何读取Analog Devices的ADXL3xx系列加速 ...
- Android加速度传感器实现“摇一摇”,带手机振动
由于代码有点多,所以就分开写了,注释还算详细,方便学习 Activity package com.lmw.android.test; import android.app.Activity; im ...
- 玩转Android之加速度传感器的使用,模仿微信摇一摇
Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...
- Android 使用加速度传感器实现摇一摇功能及优化
如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得 ...
随机推荐
- mlpack:可伸缩C++机器学习库(转)
摘要:mlpack是一个可伸缩C++机器学习库,它的目的是让新用户通过简单.一致的API使用机器学习,同时为专业用户提供C++的高性能和最大灵活性. mlpack是一个直观.快速.可伸缩的C++机器学 ...
- Spring学习之Jar包功能介绍(转)
spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...
- linux使用wget纯命令下载JDK的方法(凑字数)
linux使用wget纯命令下载JDK的方法 linux使用wget纯命令下载JDK的方法 Oracle官网上下载jdk,需要点击accept licence的才能下载,所以一般的直接使用wget下载 ...
- Android 利用摄像头指尖测试心率
过摄像头来获得心率,搜了一下这个技术真不是噱头,据说在iPhone早有实现,主要原理是:当打开软件时,手机的闪光灯也会被自动打开,用户将手指放在摄像头上时,指尖皮下血管由于有血液被压入,被光源照射的手 ...
- 找出n个数中出现了奇数次的两个数
如果是找只出现了奇数次的一个数, 那么我们从头异或一遍就可以. 那么如何找出现了奇数次的两个数呢? 首先我们还是从头异或一遍, 然后结果肯定不为0, 对于异或出来的结果, 如果这个数的某一位是1, 说 ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- Oracle字符集转换
这几天在工作中碰到一个字符乱码的问题,发现在cmd窗口的sqlplus中直接update一个中文和使用@调用一个文件作同样更新的时候,存储的结果 竟不一样.一时比较迷惑,对Oracle ...
- Thread-safety with regular expressions in Java
As mentioned in our introduction to the Pattern and Matcher classes, the Java regular expression API ...
- 激活工具 – Microsoft Toolkit 2.4.7
Microsoft Toolkit是一款很出名的Windows/Office激活工具,最早是因为激活Office 2010出名的,想必不少人也用过吧?Microsoft Toolkit从2.4.1版本 ...
- 20个经典bootsrtap后台html网站模板推荐
今天为大家推荐20款不同风格的Bootstrap后台管理模板,每一款都经典可用,能预览和下载,保证让你挑得眼花缭乱. 1,Simpli flag蓝色 Simpli Flat蓝色管理模板是一款采用Fla ...