android加速度传感器简单实现
来源:互联网 发布:button按钮跳转js函数 编辑:程序博客网 时间:2024/06/04 17:41
1、Activity类
public class MainActivity extends Activity {private TextView content;private Button btn;private boolean flag = false;private float max = 0;private float min = 0;private long startTime = -1;private Random r = new Random();// 抽签private String[] allResults = { "大吉", "小吉", "凶", "大凶" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);content = (TextView) findViewById(R.id.content);btn = (Button) findViewById(R.id.btn);SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);// 建立传感器对象Sensor s = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// 设置监听,当传感器中的值有变化时,自动进行监听反馈manager.registerListener(new SensorEventListener() {@Overridepublic void onSensorChanged(SensorEvent event) {if (flag) {if (startTime == -1) {startTime = System.currentTimeMillis();}// 当有变化时,自动执行此方法// 通过event可以取得三个方向的加速度值float[] values = event.values;// StringBuilder builder = new StringBuilder();// builder.append(values[0] + "\r\n");// builder.append(values[1] + "\r\n");// builder.append(values[2] + "\r\n");//// content.setText(builder.toString());if (values[0] > max) {max = values[0];}if (values[0] < min) {min = values[0];}if (max > 10 && min < -10) {// 可以出结果了flag = false;// 随机生成int index = r.nextInt(4);content.setText("结果为:" + allResults[index]);startTime = -1;}if (System.currentTimeMillis() - startTime > 2000) {flag = false;startTime = -1;Toast.makeText(MainActivity.this, "超时,请重新摇",Toast.LENGTH_SHORT).show();}}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}}, s, SensorManager.SENSOR_DELAY_GAME);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {flag = true;content.setText("请开始摇");}});}}2、布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:layout_alignParentBottom="true" /></RelativeLayout>
0 0
- android加速度传感器简单实现
- Android加速度传感器实现微信“摇一摇”
- Android传感器之加速度传感器
- android加速度传感器
- Android加速度传感器
- android手机加速度传感器
- android光照、加速度传感器
- Android加速度传感器原理
- android 加速度传感器,重力传感器,线性加速度传感器
- 加速度传感器的简单使用
- Android加速度传感器实现“摇一摇”,带手机振动
- Android 使用加速度传感器实现摇一摇功能及优化
- Android游戏开发中加速度传感器的实现
- Android加速度传感器的使用:摇一摇功能的实现
- Android加速度传感器的使用:摇一摇功能的实现
- android 通过加速度传感器实现手机来电时翻转静音
- Android 加速度传感器(G-Sensor)
- Android 加速度传感器(G-Sensor)
- 推荐30款最佳的数据可视化工具
- DHT网络原理制作bt采集蜘蛛,开源版
- Myeclipse重装后的必要配置
- RedHat下挂载U盘
- Bloom Filter概念和原理
- android加速度传感器简单实现
- Linux swapper进程 init进程
- LeetCode | Linked List Cycle
- 常用RGB颜色表
- Swap函数老是出问题,说明理解的不透彻
- gridview 显示大量 bitmap时候出的问题
- 插入排序
- HTML自学日志 (1)
- Median of Two Sorted Arrays - 寻找两个有序数组的中位数(重)