Android方向传感器实践——自己动手做水平尺

 

这是基于上一篇博文Android方向传感器实践(指南针)的后续探索,这里将会为大家介绍如何利用方向传感器Y轴上的读数制作一个水平尺,具体的代码和源文件将会全部和大家分享。

 

 

Android方向传感器实践——自己动手做水平尺_第1张图片

 

Android方向传感器实践——自己动手做水平尺_第2张图片

 

 

 

 

下面附上LevelActivity类和LevelView类的代码,同时提供源程序的下载,如果有什么问题的话随时可以与我联系,我的qq:264965161,电子邮箱[email protected],本人非常乐意与大家一起交流,希望大家能共同进步。

 

源代码下载地址:http://download.csdn.net/source/2933499

 

package com.app; import android.app.Activity; import android.hardware.SensorManager; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import java.util.List; import android.content.pm.ActivityInfo; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class LevelActivity extends Activity implements SensorEventListener { private float _srcDegree = 0; private LevelView _levelView; private boolean mRegisteredSensor; //定义SensorManager private SensorManager mSensorManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置为横向 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置为无标题栏 requestWindowFeature( Window.FEATURE_NO_TITLE ); //设置为全屏模式 getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN ); _levelView = new LevelView(this); setContentView(_levelView); mRegisteredSensor = false; //取得SensorManager实例 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @Override protected void onResume() { super.onResume(); //接受SensorManager的一个列表(Listener) //这里我们指定类型为TYPE_ORIENTATION(方向感应器) List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION); if (sensors.size() > 0) { Sensor sensor = sensors.get(0); //注册SensorManager //this->接收sensor的实例 //接收传感器类型的列表 //接受的频率 mRegisteredSensor = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST); } } @Override protected void onPause() { if (mRegisteredSensor) { //如果调用了registerListener //这里我们需要unregisterListener来卸载/取消注册 mSensorManager.unregisterListener(this); mRegisteredSensor = false; } super.onPause(); } //当进准度发生改变时 //sensor->传感器 //accuracy->精准度 @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } // 当传感器在被改变时触发 @Override public void onSensorChanged(SensorEvent event) { // 接受方向感应器的类型 if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { // 这里我们可以得到数据,然后根据需要来处理 float y = event.values[SensorManager.DATA_Y]; if(Math.abs(y - _srcDegree) < 50) { _levelView.setDegree(y); _srcDegree = y; } } } }

 

package com.app; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.view.View; public class LevelView extends View implements Runnable { private Paint _mPaint = new Paint(); private Bitmap _level; private Bitmap _graduation; private String _message = "0.0°"; private float _decDegree = 0; private int _screenWidth = 480; public LevelView(Context context) { super(context); // 载入图片 _level = BitmapFactory.decodeResource(getResources(), R.drawable.rl001); _graduation = BitmapFactory.decodeResource(getResources(), R.drawable.rl002); // 开启线程否则无法更新画面 new Thread(this).start(); } @Override public void run() { while(!Thread.currentThread().isInterrupted()) { try { Thread.sleep(100); } catch(InterruptedException e) { Thread.currentThread().interrupt(); } postInvalidate(); } } protected void onDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); _mPaint.setColor(Color.WHITE); _mPaint.setTextSize(30); _mPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG); // 横向距离161 //float dx = 322 / 180; float dx = 1.78f; System.out.println(dx); // 设置几何变换矩阵 Matrix mat = new Matrix(); if(_decDegree < -90) { _decDegree = -90; } if(_decDegree > 90) { _decDegree = 90; } _decDegree = _decDegree + 90; mat.setTranslate(362 - (dx * _decDegree), 106); // 绘制图像 canvas.drawBitmap(_graduation, 0, 0, null); canvas.drawBitmap(_level, mat, _mPaint); canvas.drawText(_message, 80, 85, _mPaint); } // 更新水平尺刻度 public void setDegree(float degree) { _decDegree = degree; String degreeStr = String.valueOf(_decDegree); _message = degreeStr + "°"; } }

你可能感兴趣的:(thread,android,Class,float,Matrix,delay)