Android重力感应G-sensor

概念


使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下。

在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下。注意: 下面提供的demo程序只能在有重力感应的真机上跑。

 

重力感应坐标系


看一下模拟图:

以屏幕的左下方为原点,箭头指向的方向为正。从-10到10,以浮点数为等级单位(2D编程的时候,是以屏幕左上方为原点的,这是和3D不一样的地方)

xyz值规则就是:朝天的就是正数(以屏幕的左下角和地平行的面为基础面,在这个面之上,距离越远值越大,在这个面之下,距离越远值越小),朝地的就是负数。利用x,y,z三个值求三角函数,就可以精确检测手机的运动状态了。

 

所以理论上:

手机屏幕向上(z轴朝天)水平放置的时侯,(x,y,z)的值分别为(0,0,10);

手机屏幕向下(z轴朝地)水平放置的时侯,(x,y,z)的值分别为(0,0,-10);

手机屏幕向左侧放(x轴朝天)的时候,(x,y,z)的值分别为(10,0,0);

手机竖直(y轴朝天)向上的时候,(x,y,z)的值分别为(0,10,0);

 

源代码


复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="40dip" />
    
    <TextView
        android:id="@+id/texty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="40dip" />
    
    <TextView
        android:id="@+id/textz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="40dip" />
</LinearLayout>
复制代码
复制代码
package com.example.layout;

import android.support.v7.app.ActionBarActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    private SensorManager sensorMgr;
    Sensor sensor;
    TextView textX = null;
    TextView textY = null;
    TextView textZ = null;

    private float x, y, z;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        textX = (TextView) findViewById(R.id.textx);
        textY = (TextView) findViewById(R.id.texty);
        textZ = (TextView) findViewById(R.id.textz);

        SensorEventListener lsn = new SensorEventListener() {
            public void onSensorChanged(SensorEvent e) {
                x = e.values[SensorManager.DATA_X];
                y = e.values[SensorManager.DATA_Y];
                z = e.values[SensorManager.DATA_Z];
                setTitle("x=" + (int) x + "," + "y=" + (int) y + "," + "z="+ (int) z);
                textX.setText("x=" + (int) x);
                textY.setText("y=" + (int) y);
                textZ.setText("z=" + (int) z);
            }
            public void onAccuracyChanged(Sensor s, int accuracy) {
            }
        };
        // 注册listener,第三个参数是检测的精确度
        sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
    }
}
复制代码

 

 

实际的结果看一下


水平的放在桌子上:

 

正立在桌子上:

 

侧立在桌子上:

 

随意的放置:

 

 

这下清楚了吧~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 手机系统中的重力传感器+源码,作者信息:Himi,重力传感器也称为加速度传感器,源代码编译环境需要SDK 1.5(api 3)支持。   此传感器不仅对玩家反转手机的动作可以检测到,而且会根据反转手机的程度,得到传感器的值也会不同!      部分源代码释义:   通过服务得到传感器管理对象   sm = (SensorManager) MainActivity.ma.getSystemService(Service.SENSOR_SERVICE);   sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//得到一个重力传感器实例   //TYPE_ACCELEROMETER 加速度传感器(重力传感器)类型。   //TYPE_ALL 描述所有类型的传感器。   //TYPE_GYROSCOPE 陀螺仪传感器类型   //TYPE_LIGHT 光传感器类型   //TYPE_MAGNETIC_FIELD 恒定磁场传感器类型。   //TYPE_ORIENTATION 方向传感器类型。   //TYPE_PRESSURE 描述一个恒定的压力传感器类型   //TYPE_PROXIMITY 常量描述型接近传感器   //TYPE_TEMPERATURE 温度传感器类型描述   mySensorListener = new SensorEventListener() {   @Override   //传感器获取值发生改变时在响应此函数    public void onSensorChanged(SensorEvent event) {//备注1    //传感器获取值发生改变,在此处理    x = event.values[0]; //手机横向翻滚    //x>0 说明当前手机左翻 x0 说明当前手机下翻 y0 手机屏幕朝上 z<0 手机屏幕朝下    arc_x -= x;//备注2    arc_y += y;   }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值