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.首先获得 ...
随机推荐
- mina学习资料整合
最好的资料当然是官方文档:https://mina.apache.org/mina-project/userguide/user-guide-toc.html 官方文档,配合源码中的example例子 ...
- 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程
想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...
- R与数据分析旧笔记(十四) 动态聚类:K-means
动态聚类:K-means方法 动态聚类:K-means方法 算法 选择K个点作为初始质心 将每个点指派到最近的质心,形成K个簇(聚类) 重新计算每个簇的质心 重复2-3直至质心不发生变化 kmeans ...
- Anndroid 开发架构读书笔记
市面上大部分应用不外乎就是颠过来倒过去地做以下这些事情: --------------- --------------- --------------- --------------- | | | | ...
- vb.net 操作xml
xml文件: <?xml version="1.0" encoding="gb2312"?> <bookstore> <book ...
- POJ 3261 Milk Patterns(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过 ...
- JAVA异常设计原则
异常是面向对象语言非常重要的一个特性,良好的异常设计对程序的可扩展性.可维护性.健壮性都起到至关重要. JAVA根据用处的不同,定义了两类异常 * Checked Exception: Exc ...
- 解决Agent admitted failure to sign using the kye with ssh
之前如果建立 ssh 连接,只要將公匙复制到~/.ssh/authorized_keys就可以直接登录而不需要建立密碼. 如果在使用时候出现如下信息: Agent admitted failure t ...
- 浅析linux中的fork、vfork和clone
各种大神的混合,做个笔记. http://blog.sina.com.cn/s/blog_7598036901019fcg.html http://blog.csdn.net/kennyrose/ar ...
- Candy----HDU4465----数学题
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4465 题目意思: 有两个箱子,每个箱子装有N个糖果 打开第一个箱子的概率是P,另外一个就是1-P 当小 ...