Android 高仿微信界麵
https://blog.csdn.net/dawanganban/article/details/20408505
上一篇《是男人就下100層【第一層】——高仿微信界麵(7)》中我們實現了下彈式菜單,這一篇我們來看看如何實現微信中的搖一搖功能。
首先我們來布局我們的搖一搖界麵
布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://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>
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通過getDefaultSensor(int type)方法來獲取指定類型的傳感器。
- // 獲得重力傳感器
- sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- sensorManager.registerListener(this, sensor,
- SensorManager.SENSOR_DELAY_GAME);
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(){
- @Override
- public 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);
- 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);
- }
源代碼下載:https://download.csdn.net/detail/lxq_xsyu/6990129
最後更新:2017-04-03 12:55:22