8-2 センサ関係(加速度センサ、地磁気センサ)

Download Report

Transcript 8-2 センサ関係(加速度センサ、地磁気センサ)

8-2 センサ関係
1.センサの種類
種類
TYPE_ACCELEROMETER
TYPE_MAGNETIC_FIELD
TYPE_PROXIMITY
TYPE_TEMPATURE
TYPE_LIGHT
TYPE_GRAVITY
TYPE_PRESSURE
TYPE_GYROSCOPE
TYPE_LINEAR_ACCELERATION
TYPE_ROTAION_VECTOR
センサの内容
加速度センサ
地磁気センサ
近接センサ
温度センサ
照明センサ
重力センサ
圧力センサ
ジャイロスコープ
直線加速度センサ
回転ベクトル
2.センサの検知速度
SensorManagerの定数
種類
SENSOR_DELAY_FASTEST
SENSOR_DELAY_GAME
SENSOR_DELAY_UI
SENSOR_DELAY_NORMAL
定数
早い
ゲームに適する速さ
ユーザインターフェースに適する速さ
通常の速さ
3.加速度センサの例
(端末を早く動かすと画像が濃くなる例)
A.関連クラス
クラス
概
要
android.widget.ImageViewクラス
void setAlpha()
android.hardware.SensorManagerクラス
不透明度設定
Sensor getDefaultSensor(int type)
boolean registerListener(SensorEventListener e,
Sensor s, int rate)
void unregisterListener(SensorEventListener e)
android.hardware.Sensorクラス
センサー取得
センサイベントリスナを登録
int getType()
android.hardware.SensorEventクラス
センサ種類を取得
Sensor sensor
float[] values
センサを示すフィールド
センサの値を表す配列
センサイベントリスナを解除
B.プログラム例(その1)
package jp.sensor;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.graphics.*;
import android.hardware.*;
public class SensorActivity extends Activity {
ImageView imageV; SensorManager sensorM; Sensor sensor;
AccSensorEventListener sse; public float val;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);
setContentView(LL);
プログラム例(その2)
LL.setGravity(Gravity.CENTER);
setContentView(LL);
Bitmap bmp=BitmapFactory.decodeResource(getResources(),
R.drawable.leaf);
imageV = new ImageView(this); imageV.setImageBitmap(bmp);
imageV.setAlpha(100); LL.addView(imageV);
sse = new AccSensorEventListener();
}
protected void onResume(){
super.onResume();
sensorM =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensor=sensorM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorM.registerListener(sse, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
プログラム例(その3)
protected void onPause(){
super.onPause();
sensorM.unregisterListener(sse);
}
class AccSensorEventListener implements SensorEventListener{
public void onSensorChanged(SensorEvent e){
if(e.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
float tmp=e.values[0]+e.values[1]+e.values[2];
if ((tmp - val)>2) imageV.setAlpha(255);
else imageV.setAlpha(100);
val=tmp;
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {}
}
}
4.地磁気センサの例
(地磁気センサで方角を知る)
A.関連クラス
クラス
概
要
android.hardware.SensorManagerクラス
static boolean getRotationMatrix(float[] rot,
回転行列を取得
float[] I, float[]gravity, float[] geomagnetic)
static boolean remapCoordinateSystem(
座標変換
float[] inR, int X, int Y, float[] outR)
static float[] getOrientaion(float[]R, float[] v) 傾きを取得
B.プログラム例(その1)
package jp.sensor;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import android.widget.*;
import android.graphics.*;
import android.hardware.*;
public class DirSensorActivity extends Activity {
Bitmap bmp; ImageView imageV; SensorManager sensorM;
Sensor sensor1, sensor2; DirSensorEventListener dse;
float[]accV=new float[3]; float[]magV=new float[3];
float[]rotMat1=new float[16]; float[]rotMat2=new float[16];
float[]I=new float[16];
float[]V=new float[3];
B.プログラム例(その1)
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout LL=new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);
LL.setGravity(Gravity.CENTER);
setContentView(LL);
bmp=BitmapFactory.decodeResource(getResources(),
R.drawable.image001);
imageV=new ImageView(this);
imageV.setImageBitmap(bmp);
LL.addView(imageV);
dse=new DirSensorEventListener();
}
B.プログラム例(その1)
protected void onResume(){
super.onResume();
sensorM=(SensorManager)
getSystemService(Context.SENSOR_SERVICE);
sensor1=sensorM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensor2=sensorM.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorM.registerListener(
dse,sensor1,SensorManager.SENSOR_DELAY_NORMAL);
sensorM.registerListener(
dse,sensor2,SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause(){
super.onPause();
sensorM.unregisterListener(dse,sensor1);
sensorM.unregisterListener(dse,sensor2);
}
B.プログラム例(その1)
class DirSensorEventListener implements SensorEventListener{
public void onAccuracyChanged(Sensor s, int acc){}
public void onSensorChanged(SensorEvent e) {
switch(e.sensor.getType())
{
case Sensor.TYPE_MAGNETIC_FIELD :
magV=e.values.clone(); break;
case Sensor.TYPE_ACCELEROMETER :
accV=e.values.clone(); break;
}
if(magV != null && accV !=null){
//回転行列の取得
SensorManager.getRotationMatrix(rotMat1, I, accV,magV);
SensorManager.remapCoordinateSystem(
rotMat1, SensorManager.AXIS_X,
SensorManager.AXIS_Z,rotMat2);
SensorManager.getOrientation(rotMat2, V);
B.プログラム例(その1)
float d=(float)Math.toDegrees(V[0]);
Matrix m=new Matrix();
m.postRotate(-d);
Bitmap tmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),m,true);
imageV.setImageBitmap(tmp);
}
}
}
}