上一篇《是男人就下100層【第一層】——高仿介面(7)》中我們實現了下彈式菜單,這一篇我們來看看如何?中的搖一搖功能。
首先我們來布局我們的搖一搖介面
布局檔案如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#111" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" > <ImageView android:id="@+id/shakeBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/shakehideimg_man2" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <RelativeLayout android:id="@+id/shakeImgUp" android:layout_width="fill_parent" android:layout_height="190dp" android:background="#111"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:src="@drawable/shake_logo_up" /> </RelativeLayout> <RelativeLayout android:id="@+id/shakeImgDown" android:layout_width="fill_parent" android:layout_height="190dp" android:background="#111"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@drawable/shake_logo_down" /> </RelativeLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/shake_title_bar" android:layout_width="fill_parent" android:layout_height="45dp" android:background="@drawable/title_bar" android:gravity="center_vertical" > <Button android:layout_width="70dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="返回" android:textSize="14sp" android:textColor="#fff" android:onClick="shake_activity_back" android:background="@drawable/title_btn_back"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搖一搖" android:layout_centerInParent="true" android:textSize="20sp"android:textColor="#ffffff" /> <ImageButton android:layout_width="67dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:src="@drawable/mm_title_btn_menu"android:background="@drawable/title_btn_right"android:onClick="linshi" /> </RelativeLayout> <SlidingDrawer android:id="@+id/slidingDrawer1" android:layout_width="match_parent" android:layout_height="match_parent" android:content="@+id/content" android:handle="@+id/handle" > <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shake_report_dragger_up" /> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f9f9f9" > <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/shake_line_up" /> </LinearLayout> </SlidingDrawer></RelativeLayout>
這個布局裡面用到了很多相對布局,最外面是一個相對布局(我們先稱為R1),R1內是兩個相對布局(分別稱為R2、R3)和一個抽屜組件,R3是上部標題列,R2中有一個ImageView和一個線性布局,這兩個組件都位於R2的中心,所以上面的線性布局會將下面的ImageView遮住,為什麼要遮住,玩過搖一搖的朋友應該明白。
SlidingDrawer隱藏屏外的內容,並允許使用者通過handle以顯示隱藏內容。它可以垂直或水平滑動,它有倆個View組成,其一是可以拖動的handle,其二是隱藏內容的View.它裡面的控制項必須設定布局,在布局檔案中必須指定handle和content.
接下來我們看看如何檢查手機搖晃,搖一搖讓手機震動
package com.example.weixin.listener;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.util.Log;/** * 一個檢測手機搖晃的監聽器 */public class ShakeListener implements SensorEventListener {// 速度閾值,當搖晃速度達到這值後產生作用private static final int SPEED_SHRESHOLD = 3000;// 兩次檢測的時間間隔private static final int UPTATE_INTERVAL_TIME = 70;// 感應器管理器private SensorManager sensorManager;// 感應器private Sensor sensor;// 重力感應監聽器private OnShakeListener onShakeListener;// 上下文private Context mContext;// 手機上一個位置時重力感應座標private float lastX;private float lastY;private float lastZ;// 上次檢測時間private long lastUpdateTime;// 構造器public ShakeListener(Context c) {// 獲得監聽對象mContext = c;start();}// 開始public void start() {// 獲得感應器管理器sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);if (sensorManager != null) {// 獲得重力感應器sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);}// 註冊if (sensor != null) {sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_GAME);}}// 停止檢測public void stop() {sensorManager.unregisterListener(this);}// 設定重力感應監聽器public void setOnShakeListener(OnShakeListener listener) {onShakeListener = listener;}// 重力感應器感應獲得變化資料public void onSensorChanged(SensorEvent event) {// 現在檢測時間long currentUpdateTime = System.currentTimeMillis();// 兩次檢測的時間間隔long timeInterval = currentUpdateTime - lastUpdateTime;// 判斷是否達到了檢測時間間隔if (timeInterval < UPTATE_INTERVAL_TIME)return;// 現在的時間變成last時間lastUpdateTime = currentUpdateTime;// 獲得x,y,z座標float x = event.values[0];float y = event.values[1];float z = event.values[2];// 獲得x,y,z的變化值float deltaX = x - lastX;float deltaY = y - lastY;float deltaZ = z - lastZ;// 將現在的座標變成last座標lastX = x;lastY = y;lastZ = z;double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ* deltaZ)/ timeInterval * 10000;//Log.v("thelog", "===========log===================");// 達到速度閥值,發出提示if (speed >= SPEED_SHRESHOLD) {onShakeListener.onShake();}}public void onAccuracyChanged(Sensor sensor, int accuracy) {}// 搖晃監聽介面public interface OnShakeListener {public void onShake();}}SensorManager是一個系統提供來管理感應器的服務。
SensorManager通過getDefaultSensor(int type)方法來擷取指定類型的感應器。
// 獲得重力感應器sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorManager提供了一個註冊感應器的方法:registerListener
sensorManager.registerListener(this, sensor,SensorManager.SENSOR_DELAY_GAME);
SensorEventListener介面定義了兩個方法需要實現
onSensorChanged()方法,當感應器的值發生改變時觸發該方法。
onAccuracyChanged()方法,當感應器的精度發生改變時觸發該方法。
// 重力感應器感應獲得變化資料public void onSensorChanged(SensorEvent event) {// 現在檢測時間long currentUpdateTime = System.currentTimeMillis();// 兩次檢測的時間間隔long timeInterval = currentUpdateTime - lastUpdateTime;// 判斷是否達到了檢測時間間隔if (timeInterval < UPTATE_INTERVAL_TIME)return;// 現在的時間變成last時間lastUpdateTime = currentUpdateTime;// 獲得x,y,z座標float x = event.values[0];float y = event.values[1];float z = event.values[2];// 獲得x,y,z的變化值float deltaX = x - lastX;float deltaY = y - lastY;float deltaZ = z - lastZ;// 將現在的座標變成last座標lastX = x;lastY = y;lastZ = z;double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ* deltaZ)/ timeInterval * 10000;//Log.v("thelog", "===========log===================");// 達到速度閥值,發出提示if (speed >= SPEED_SHRESHOLD) {onShakeListener.onShake();}}座標組應的關係看可知
這裡計算速度的公式是高中學過的知識v=s/t=(a^2+b^2+c^2)/t
double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ* deltaZ)/ timeInterval * 10000;
在Activity中建立搖動監聽執行個體並設定監聽
mShakeListener = new ShakeListener(this); mShakeListener.setOnShakeListener(new OnShakeListener() {public void onShake() {//Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到在同一時刻搖一搖的人。\n再試一次吧!", Toast.LENGTH_SHORT).show();startAnim(); //開始 搖一搖手掌動畫mShakeListener.stop();startVibrato(); //開始 震動new Handler().postDelayed(new Runnable(){@Overridepublic void run(){//Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到\n在同一時刻搖一搖的人。\n再試一次吧!", 500).setGravity(Gravity.CENTER,0,0).show();Toast mtoast;mtoast = Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到\n在同一時刻搖一搖的人。\n再試一次吧!", 10); //mtoast.setGravity(Gravity.CENTER, 0, 0); mtoast.show(); mVibrator.cancel(); mShakeListener.start();}}, 2000);}});
在設定監聽之前,在Activity的onCreate方法中需呀獲得系統提供的震動服務物件
Vibrator mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);
有關Vibrator的使用請看我的另一篇博文:http://blog.csdn.net/dawanganban/article/details/17531697
public void startVibrato(){//定義震動mVibrator.vibrate( new long[]{500,200,500,200}, -1); //第一個{}裡面是節奏數組, 第二個參數是重複次數,-1為不重複,非-1俄日從pattern的指定下標開始重複}搖一搖的時候還有一個圖片移動的動畫,設定動畫代碼如下:
public void startAnim () { //定義搖一搖動畫動畫AnimationSet animup = new AnimationSet(true);TranslateAnimation mytranslateanimup0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.5f);mytranslateanimup0.setDuration(1000);TranslateAnimation mytranslateanimup1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,+0.5f);mytranslateanimup1.setDuration(1000);mytranslateanimup1.setStartOffset(1000);animup.addAnimation(mytranslateanimup0);animup.addAnimation(mytranslateanimup1);mImgUp.startAnimation(animup);AnimationSet animdn = new AnimationSet(true);TranslateAnimation mytranslateanimdn0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,+0.5f);mytranslateanimdn0.setDuration(1000);TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.5f);mytranslateanimdn1.setDuration(1000);mytranslateanimdn1.setStartOffset(1000);animdn.addAnimation(mytranslateanimdn0);animdn.addAnimation(mytranslateanimdn1);mImgDn.startAnimation(animdn);}運行效果如下:
原始碼下載:http://download.csdn.net/detail/lxq_xsyu/6990129