程序运行闪图界面以及传感器
来源:互联网 发布:excel数据有效性颜色 编辑:程序博客网 时间:2024/05/01 02:01
概要
主要讲一些程序开始时会有一个欢迎界面,不进行任何操作过一段时间之后会自己消失,还有就是简要介绍一些android的传感器,它们很有用,比如可以制作水平仪(和一些体感游戏)指南针,甚至有的手机支持温度和大气压强
效果
有人会问了:你这个怎么什么都没有,是不是骗人的=w=,因为虚拟机不支持传感器所以什么都没有,发一张我的手机上的截图吧
这里主要说一下方向传感器,其他的很好就试出来了,比如光强你可以打开灯和关上灯来测试,方向传感器的话,我们知道手机是一个长方体,有三个方向互相垂直的面,分别沿这三个面旋转就会看到数值的变化,最后当指向北时三个值都会为零
知识点整理
1.其实所谓的闪屏就是一个计时器计时,规定几秒之后跳转
2.传感器收手机自带的功能,我们使用接口就可以了
3.Activity的生命周期,这里会涉及到,如有不明白的同学可以看这篇博文,前人之述备矣,就不再赘述了
http://blog.csdn.net/liuhe688/article/details/6733407DEMO
为了防止文章又臭又长就只发关键代码了~
首先界面不用说,没有什么特别的地方,我们来看一下主界面的activity是如何实现计时跳转的
package com.example.administrator.lyfs;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Window;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); final Intent main = new Intent(this,MainChoice.class); Timer timer = new Timer(); TimerTask timertask = new TimerTask() { @Override public void run() { startActivity(main); MainActivity.this.finish(); } }; timer.schedule(timertask,1000*3); }}
就是使用timer来实现这个功能的,代码简单看上去又高端大气~
第二个界面只有一个按钮,跳转到第三个界面,因为第二个界面以后要做其他东西
第三个界面的java代码如下
package com.example.administrator.lyfs;import android.app.Activity;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.widget.TextView;/** * Created by Administrator on 2015/7/18. */public class SensorTest extends Activity implements SensorEventListener{ private SensorManager mSensorManger; TextView firstOne; TextView firstTwo; TextView firstThree; TextView secondOne; TextView secondTwo; TextView secondThree; TextView ThirdOne; TextView ForthOne; TextView FifthOne; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activirty_sensor_test); firstOne = (TextView)findViewById(R.id.first_first); firstTwo = (TextView)findViewById(R.id.first_second); firstThree = (TextView)findViewById(R.id.first_third); secondOne = (TextView)findViewById(R.id.second_first); secondTwo = (TextView)findViewById(R.id.second_second); secondThree = (TextView)findViewById(R.id.second_third); ThirdOne = (TextView)findViewById(R.id.third_first); ForthOne = (TextView)findViewById(R.id.forth_first); FifthOne = (TextView)findViewById(R.id.fifth_first); mSensorManger = (SensorManager)getSystemService(SENSOR_SERVICE); } @Override protected void onResume() { super.onResume(); mSensorManger.registerListener(this, mSensorManger.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME); mSensorManger.registerListener(this, mSensorManger.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME); mSensorManger.registerListener(this, mSensorManger.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE), SensorManager.SENSOR_DELAY_GAME); mSensorManger.registerListener(this, mSensorManger.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME); mSensorManger.registerListener(this, mSensorManger.getDefaultSensor(Sensor.TYPE_PRESSURE), SensorManager.SENSOR_DELAY_GAME); } @Override protected void onStop() { mSensorManger.unregisterListener(this); super.onStop(); } @Override protected void onPause() { mSensorManger.unregisterListener(this); super.onPause(); } @Override public void onAccuracyChanged(Sensor sensor,int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { float[] values = event.values; int senseorType = event.sensor.getType(); switch (senseorType) { case Sensor.TYPE_ORIENTATION: firstOne.setText(""+values[0]); firstTwo.setText(""+values[1]); firstThree.setText(""+values[2]); break; case Sensor.TYPE_MAGNETIC_FIELD: secondOne.setText(""+values[0]); secondTwo.setText(""+values[1]); secondThree.setText(""+values[2]); break; case Sensor.TYPE_AMBIENT_TEMPERATURE: ThirdOne.setText(""+values[0]); break; case Sensor.TYPE_LIGHT: ForthOne.setText(""+values[0]); break; case Sensor.TYPE_PRESSURE: FifthOne.setText(""+values[0]); break; } }}
我们是通过activity实现SensorEventListener接口来实现的,这个接口需要重载两个方法
1.public void onAccuracyChanged(Sensor sensor,int accuracy) 当精度变化时调用这个函数
2.public void onSensorChanged(SensorEvent event)当传感器感应数值变化时调用
总的来说我们在Activity中做了这几件事
0.实现SensorEventListener接口
1.获得布局文件里的TextView用来显示数值
2.我们需要一个sensorManager来管理传感器
3.在onResume里注册传感器
4.在onPause和onStop里注销传感器(为了节省资源)
5.在onSensorChanged里面通过事件(event) 获得相关数据(value)然后通过传感器种类确定相应的显示数值
感想
这就是手机传感器的功能,一般的手机可能没有温度和压力传感器,一共有11个传感器
1.加速度传感器 Sensor.TYPE_ACCELEROMETER:
2.磁场传感器 Sensor.TYPE_MAGNETIC_FIELD:#
3.陀螺仪感应器 Sensor.TYPE_GYROSCOPE:
4.光线传感器 Sensor.TYPE_LIGHT:#
5.距离感应器Sensor.TYPE_PROXIMITY:
6.重力传感器Sensor.TYPE_GRAVITY:
7.线性加速传感器Sensor.TYPE_LINEAR_ACCELERATION:
8.旋转向量传感器Sensor.TYPE_ROTATION_VECTOR:
9.方向传感器:Sensor.TYPE_ORIENTATION:#
2.磁场传感器 Sensor.TYPE_MAGNETIC_FIELD:#
3.陀螺仪感应器 Sensor.TYPE_GYROSCOPE:
4.光线传感器 Sensor.TYPE_LIGHT:#
5.距离感应器Sensor.TYPE_PROXIMITY:
6.重力传感器Sensor.TYPE_GRAVITY:
7.线性加速传感器Sensor.TYPE_LINEAR_ACCELERATION:
8.旋转向量传感器Sensor.TYPE_ROTATION_VECTOR:
9.方向传感器:Sensor.TYPE_ORIENTATION:#
10.温度传感器Sensor.TYPE_TEMPERATURE:#
11.压力传感器,Sensor.TYPE_PRESSURE#
11.压力传感器,Sensor.TYPE_PRESSURE#
画#的是我们用过的,不过用法大同小异,大家可以试试,知道了这些物理事件可大有干头,我们以后也会用其他的传感器做些东西,敬请期待~
每日一句
真正的爱情不是一时好感,而是明明知道没结果,还想要坚持下去的冲动
0 0
- 程序运行闪图界面以及传感器
- 隐藏运行程序界面
- Android自定义启动界面以及初次运行界面
- 为何有时候运行程序,界面一闪而过
- .net程序界面运行为什么会变形
- pb 制作程序运行报错界面
- 程序可以运行,但几个界面打不开
- 让MFC程序隐藏运行界面
- 让MFC程序隐藏运行界面
- 让MFC程序隐藏运行界面
- 让MFC程序隐藏运行界面
- 让MFC程序隐藏运行界面
- android 正在运行的程序与界面
- 初学者dos界面编译运行Java程序
- 程序运行时鼠标滚轮缩放界面
- ubuntu 下编写程序以及运行可执行文件
- ios程序运行过程以及常见文件
- Activity运行状态以及完全退出程序
- Linux键盘快捷键
- eclipse安装svn插件图文
- Android初学之路(二)
- 什么是设计模式?为什么要使用设计模式?有什么好处?
- iOS如何在一个图标上加上气泡,显示数字
- 程序运行闪图界面以及传感器
- angularjs开发常见问题-2(angularjs内置过滤器)
- Container With Most Water
- [LeetCode]Rectangle Area
- 深度学习(Deep Learning,DL)的相关资料总结
- python操作MySQL数据库
- 6. 重建二叉树
- 递归和递推
- Merge Sorted Array