Android的重力传感器(3轴加速度传感器)简单实例

时间:2022-07-03 10:02:44

重力感应主要是依靠手机的加速度传感器(accelerometer)来实现

在Android的开发中一共有八种传感器但是不一定每一款真机都支持这些传感器。因为很多功能用户根本不care的所以可能开发商会把某些功能屏蔽掉。还是得根据真机的实际情况来做开发,今天我们主要来讨论加速度传感器的具体实现方式。

传感器名称如下:

加速度传感器(accelerometer)
陀螺仪传感器(gyroscope)
环境光照传感器(light)
磁力传感器(magnetic field)
方向传感器(orientation)
压力传感器(pressure)
距离传感器(proximity)
温度传感器(temperature)

Android的重力传感器(3轴加速度传感器)简单实例

上面的是程序的运行图

遇到的问题:

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