重力感应主要是依靠手机的加速度传感器(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轴加速度传感器)简单实例的更多相关文章

  1. 六轴加速度传感器MPU6050官方DMP库到瑞萨RL78/G13的移植

    2015年的电赛已经结束了.赛前接到器件清单的时候,看到带防护圈的多旋翼飞行器赫然在列,又给了一个瑞萨RL78/G13的MCU,于是自然联想到13年的电赛,觉得多半是拿RL78/G13做四旋翼的主控, ...

  2. 【传感器】BMA253 数字,三轴加速度传感器

    参考文档:BMA253E DataSheet 参考文档链接 密码:9new BMA253 数字,三轴加速度传感器 关键特性: 关键特性   封装方式 LGA封装(12pins),长*宽(2mm*2mm ...

  3. Android安卓下拉阻尼效果实现原理及简单实例

    原理  这种效果是通过自定义控件的方式来实现的,我自定义了一个控件类型,这个自定义控件(PullDownDumperLayout)继承自线性布局(LinearLayout).  用户可以下拉弹出的那个 ...

  4. Android指南针之加速度传感器地磁传感器-android学习之旅(67)

    由于andorid不推荐用传统的方向传感器,推荐用加速度传感器和地磁传感器来构造得到方向传感器的数据,其实主要是z轴的旋转角度 具体代码示例 代码如下 public class MainActivit ...

  5. OneNET麒麟座应用开发之五:获取加速度传感器ADXL345数据

    由于数据采集站基本都安装在野外或者楼顶,安装位置以及震动对检测数据的准确性有一定影响.所以想要有一个位置状态数据,正好发现麒麟作上有ADXL345,这样一个数字输出的加速度传感器.如图中红框所示: 1 ...

  6. ADXL3xx: 读取 ADXL3xx 加速度传感器

    原文链接:https://www.arduino.cc/en/Tutorial/ADXL3xx ADXL3xx加速度传感器 本教程将为你展示如何读取Analog Devices的ADXL3xx系列加速 ...

  7. Android加速度传感器实现“摇一摇”,带手机振动

    由于代码有点多,所以就分开写了,注释还算详细,方便学习 Activity package com.lmw.android.test;   import android.app.Activity; im ...

  8. 玩转Android之加速度传感器的使用,模仿微信摇一摇

    Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...

  9. Android 使用加速度传感器实现摇一摇功能及优化

    如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得 ...

随机推荐

  1. As Easy As A+B

    Problem Description These days, I am thinking about a question, how can I get a problem as easy as A ...

  2. C学习之结构体

    结构体(struct) 结构体是由基本数据类型构成的.并用一个标识符来命名的各种变量的组合,结构体中可以使用不同的数据类型. 1. 结构体说明和结构体变量定义 在Turbo C中, 结构体也是一种数据 ...

  3. Oracle数据库时间修改

    http://blog.csdn.net/tianlesoftware/article/details/6163859

  4. 监控Informix-Url

    jdbc:informix-sqli://[{ip-address|host-name}:{port-number|service-name}][/dbname]: INFORMIXSERVER=se ...

  5. 查GDI对象泄露的利器:GDIView

    查GDI对象泄露的利器:GDIView可以很详细的查到进程的GDI对象的总个数,详细的GDI对象的个数,以及其增减数量.其GDI对象类型也可以很详细的得知,以及其内存地址,句柄.实在是好使! 下载地址 ...

  6. Finding the Longest Palindromic Substring in Linear Time

    Finding the Longest Palindromic Substring in Linear Time Finding the Longest Palindromic Substring i ...

  7. PHP计划任务:如何使用Linux的Crontab执行PHP脚本

    我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...

  8. QVector 和vector的比较

    QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ...

  9. 汉诺塔 python版

    汉诺塔问题:如果将n个盘子(由小到大)从a通过b,搬到c,搬运过程中不能出现小盘子在大盘子下面的情况. 思路分析:假设前要移动第100个盘子,分两步走,移动第99个:再移动第100个:而要移动第99个 ...

  10. 线段树讲解(数据结构、C++)

    声明    : 仅一张图片转载于http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/2464583.html,自己画太麻烦了...那个博客的讲解也很好 ...