2014年10月22日 星期三

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

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


 


此範例為程式碼(07\Sample01)的改寫,利用一個LinearLayout一個ImageView來實作GUI界面,並透過1個ImageView+SensorManager+Sensor,利用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;
/*
 * 建立1個ImageView+SensorManager+Sensor,利用Sensor抓到的加速規訊號來控制ImageView的圖片透��度。
 */

public class MainActivity extends Activity {
ImageView iv;
SensorManager sm;//感測氣管理器
Sensor s;//感測器
SampleSensorEventListener sse;//感測器事件聆聽
float val;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setGravity(Gravity.CENTER);
setContentView(ll);

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.light);
iv= new ImageView(this);
iv.setImageBitmap(bmp);
iv.setAlpha(100);

ll.addView(iv);

sse = new SampleSensorEventListener();
}
protected void onPause()
{
super.onPause();
sm.unregisterListener(sse);
}
class SampleSensorEventListener 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)//控制圖片透��度
{
iv.setAlpha(255);
}
else
{
iv.setAlpha(100);
}
val = tmp;
}
}
public void onAccuracyChanged(Sensor s, int accuracy) {}
}
}












 




沒有留言:

張貼留言