首页 技术 正文
技术 2022年11月15日
0 收藏 939 点赞 3,526 浏览 2478 个字

概念


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

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

重力感应坐标系


看一下模拟图:

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

以屏幕的左下方为原点,箭头指向的方向为正。从-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);
}
}

实际的结果看一下


水平的放在桌子上:

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

正立在桌子上:

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

侧立在桌子上:

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

随意的放置:

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

这下清楚了吧~

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845