最近看了一下Android,只看书不总结一下,总感觉东西并没有放在脑子里,人过留名,雁过留声,遂写下这篇总结。
书本看的是《Android编程兵书》,但这篇总结的大纲是按照《第一行代码》的章节目录撰写的。两本书的内容结构大致相同,Android入门的话只看其中一本就够了。
本章内容比较易懂,重要的是如何巧妙传感器来设计一个好的APP,因此直接上代码。
简易光照探测器
public class Activity extends Activity{
private SensorManager sensorManager;
private TextView lightLevel;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lightLevel = (TextView) findViewById(R.id.lightLevel);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onDestroy(){
super.onDestroy();
if(sensorManager != null){
sensorManager.unregisterListener(listener);
}
}
private SensorEventListener listener = new SensorEventListener(){
public void onSensorChanged(SensorEvent event){
float value = event.values[0];
lightLevel.setText("Current light level is " + value + "lx");
}
public void onAccuracyChanged(Sensor sensor, int accuracy){}
}
}
加速传感器模仿微信摇一摇代码示例
public class MainActivity extends Activity{
private SensorManager sensorManager;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onDestroy(){
super.onDestroy();
if(sensorManager != null){
sensorManager.unregisterListener(listener);
}
}
private SensorEventListener listener = new SensorEventListener(){
public void onSensorChanged(SensorEvent event){
float xValue = Math.abs(event.values[0]);
float yValue = Math.abs(event.values[1]);
float zValue = Math.abs(event.values[2]);
if(xValue > 15 || yValue > 15 || zValue > 15){
Toast.makeText(MainActivity.this, "摇一摇", Toast.LENGTH_SHORT).show();
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy){}
}
}
方向传感器:判断手机在各个方向上的旋转角度(指南针,地平仪)
Android推荐的方式:通过加速传感器和地磁传感器共同计算得出手机旋转的方向和角度
简易指南针核心代码
private SensorEventListener listener = new SensorEventListener(){
float[] accelerometerValues = new float[3];
float[] magneticValues = new float[3];
private float lastRotateDegree;
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
accelerometerValues = event.values.clone();
}else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
magneticValues = event.values.clone();
}
float[] values = new float[3];
float[] R = new float[9];
//getRotationMatrix方法计算出的旋转数据赋值到R
//第二个参数用于将地磁向量转换成重力坐标的旋转矩阵,指定为null即可
//第三四个参数加速度sensor和地磁sensor输出的values
SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
//计算手机的旋转数据
// values[0] 绕Z轴旋转弧度
// values[1] 绕X轴旋转弧度
// values[2] 绕Y轴旋转弧度
SensorManager.getOrientation(R, values);
float rotateDegree = -(float)Math.toDegrees(values[0]);//弧度换成角度
if(Math.abs(rotateDegree - lastRotateDegree) > 1){
// 共六个参数
// 起始角度 终止角度
// 后四个指定旋转的中心点
RotateAnimation animation = new RotateAnimation(lastRotateDegree, rotateDegree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setFillAfter(true);
compassTmg.startAnimation(animation);
lastRotateDegree = rotateDegree;
}
}
}
网友评论