1 / 12

8-2 センサ関係 1.センサの種類

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 回転ベクトル.

peri
Download Presentation

8-2 センサ関係 1.センサの種類

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 2.センサの検知速度SensorManagerの定数 種類定数 SENSOR_DELAY_FASTEST 早い SENSOR_DELAY_GAME ゲームに適する速さ SENSOR_DELAY_UI ユーザインターフェースに適する速さ SENSOR_DELAY_NORMAL 通常の速さ

  3. 3.加速度センサの例(端末を早く動かすと画像が濃くなる例)A.関連クラス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 センサの値を表す配列

  4. 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);

  5. プログラム例(その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); }

  6. プログラム例(その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) {} } }

  7. 4.地磁気センサの例(地磁気センサで方角を知る)A.関連クラス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) 傾きを取得

  8. 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];

  9. 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(); }

  10. 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); }

  11. 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);

  12. 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); } } } }

More Related