【Android开发学习笔记】【第九课】重力感应

概念


使用重力感应技术的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);
    }
}

实际的结果看一下


水平的放在桌子上:

正立在桌子上:

侧立在桌子上:

随意的放置:

这下清楚了吧~

原文地址:https://www.cnblogs.com/by-dream/p/4143253.html