2014年10月23日 星期四

android程式設計實例入門 -Sample改寫分享 (2014/10/24)

android程式設計實例入門 -Sample改寫分享 (2014/10/24)


 


此範例為程式碼(07\Sample02)的改寫,利用一個LinearLayout一個ImageView來實作GUI界面,並透過1個ImageView+1個SensorManager+2Sensor,利用Sensor抓到的加速規訊號+電子羅盤(磁場)訊號來控制ImageView的圖片旋轉和移動,程式碼如下所示:


 


 


 


package com.jashsample;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Camera.Size;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ViewFlipper;
/*
 * 建立ImageView*1+SensorManager*1+Sensor*2,利用Sensor抓到的加速規訊號+電子羅盤(磁場)訊號來控制ImageView的圖片旋轉和移動。
 */

public class MainActivity extends Activity {
ImageView iv;
SensorManager sm;
Sensor s1, s2;
SampleSensorEventListener sse;

Bitmap bmp;
float[] av = new float[3];//加速規原始資料
float[] mv = new float[3];//電子羅盤(磁場)原始資料
float[] R1 = new float[16];
float[] R2 = new float[16];
float[] I = new float[16];
float[] v = new float[3];
@Override
protected 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.key);
iv= new ImageView(this);
iv.setImageBitmap(bmp);

ll.addView(iv);

sse = new SampleSensorEventListener();
}
protected void onResume()
{
super.onResume();
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

s1 = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//加速規
s2 = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);//電子羅盤(磁場)

sm.registerListener(sse, s1, SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(sse, s2, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause()
{
super.onPause();
sm.unregisterListener(sse, s1);//註冊事件觸發
sm.unregisterListener(sse, s2);//註冊事件觸發
}
class SampleSensorEventListener implements SensorEventListener
{
public void onSensorChanged(SensorEvent e)
{
switch (e.sensor.getType())
{
case Sensor.TYPE_MAGNETIC_FIELD://電子羅盤(磁場)
mv = e.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER://加速規
av = e.values.clone();
break;
}
if(mv != null && av != null)
{
SensorManager.getRotationMatrix(R1, I, av, mv);
SensorManager.remapCoordinateSystem(R1, SensorManager.AXIS_X, SensorManager.AXIS_Z, R2);
SensorManager.getOrientation(R2, v);
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);
iv.setImageBitmap(tmp);
}
}
public void onAccuracyChanged(Sensor s, int accuracy) {}
}
}
















 






沒有留言:

張貼留言