国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android重力感應開發之微信搖一搖功能

2019-10-22 18:10:47
字體:
來源:轉載
供稿:網友

本實例主要使用了android的重力感應功能并配合動畫效果實現,主要代碼如下:

第一:ShakeActivity主類:

package com.android.shake;  import java.io.IOException; import java.util.HashMap; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.SlidingDrawer; import android.widget.SlidingDrawer.OnDrawerCloseListener; import android.widget.SlidingDrawer.OnDrawerOpenListener; import android.widget.Toast;  import com.android.shake.ShakeListener.OnShakeListener;  public class ShakeActivity extends Activity{   ShakeListener mShakeListener = null;  Vibrator mVibrator;  private RelativeLayout mImgUp;  private RelativeLayout mImgDn;  private RelativeLayout mTitle;   private SlidingDrawer mDrawer;  private Button mDrawerBtn;  private SoundPool sndPool;  private HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>();   @Override  public void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.onCreate(savedInstanceState);  setContentView(R.layout.shake_activity);  //drawerSet ();//設置 drawer監聽 切換 按鈕的方向    mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);    mImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp);  mImgDn = (RelativeLayout) findViewById(R.id.shakeImgDown);  mTitle = (RelativeLayout) findViewById(R.id.shake_title_bar);    mDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);  mDrawerBtn = (Button) findViewById(R.id.handle);  mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener()  { public void onDrawerOpened()   {   mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_down));   TranslateAnimation titleup = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f);   titleup.setDuration(200);   titleup.setFillAfter(true);   mTitle.startAnimation(titleup);   }  });   /* 設定SlidingDrawer被關閉的事件處理 */  mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener()  { public void onDrawerClosed()   {   mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_up));   TranslateAnimation titledn = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f,Animation.RELATIVE_TO_SELF,0f);   titledn.setDuration(200);   titledn.setFillAfter(false);   mTitle.startAnimation(titledn);   }  });  loadSound() ;  mShakeListener = new ShakeListener(this);  mShakeListener.setOnShakeListener(new OnShakeListener() {   public void onShake() {   //Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到在同一時刻搖一搖的人。/n再試一次吧!", Toast.LENGTH_SHORT).show();   startAnim(); //開始 搖一搖手掌動畫   mShakeListener.stop();   sndPool.play(soundPoolMap.get(0), (float) 1, (float) 1, 0, 0,(float) 1.2);   new Handler().postDelayed(new Runnable(){    public void run(){    //Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到/n在同一時刻搖一搖的人。/n再試一次吧!", 500).setGravity(Gravity.CENTER,0,0).show();    sndPool.play(soundPoolMap.get(1), (float) 1, (float) 1, 0, 0,(float) 1.0);    Toast mtoast;    mtoast = Toast.makeText(getApplicationContext(),      "抱歉,暫時沒有找到/n在同一時刻搖一搖的人。/n再試一次吧!", 10);     //mtoast.setGravity(Gravity.CENTER, 0, 0);     mtoast.show();     mVibrator.cancel();     mShakeListener.start();         }   }, 2000);   }  });  }     private void loadSound() {   sndPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);  new Thread() {   public void run() {   try {    soundPoolMap.put(     0,     sndPool.load(getAssets().openFd(       "sound/shake_sound_male.mp3"), 1));     soundPoolMap.put(     1,     sndPool.load(getAssets().openFd(       "sound/shake_match.mp3"), 1));   } catch (IOException e) {    e.printStackTrace();   }   }  }.start();  }   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);  }  public void startVibrato(){ //定義震動  mVibrator.vibrate( new long[]{500,200,500,200}, -1); //第一個{}里面是節奏數組, 第二個參數是重復次數,-1為不重復,非-1俄日從pattern的指定下標開始重復  }   public void shake_activity_back(View v) { //標題欄 返回按鈕  this.finish();  }  public void linshi(View v) { //標題欄  startAnim();  }  @Override  protected void onDestroy() {  super.onDestroy();  if (mShakeListener != null) {   mShakeListener.stop();  }  } } 

代碼:

package com.android.shake;  import java.io.IOException; import java.util.HashMap; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.SlidingDrawer; import android.widget.SlidingDrawer.OnDrawerCloseListener; import android.widget.SlidingDrawer.OnDrawerOpenListener; import android.widget.Toast;  import com.android.shake.ShakeListener.OnShakeListener;  public class ShakeActivity extends Activity{   ShakeListener mShakeListener = null;  Vibrator mVibrator;  private RelativeLayout mImgUp;  private RelativeLayout mImgDn;  private RelativeLayout mTitle;   private SlidingDrawer mDrawer;  private Button mDrawerBtn;  private SoundPool sndPool;  private HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>();   @Override  public void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.onCreate(savedInstanceState);  setContentView(R.layout.shake_activity);  //drawerSet ();//設置 drawer監聽 切換 按鈕的方向    mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);    mImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp);  mImgDn = (RelativeLayout) findViewById(R.id.shakeImgDown);  mTitle = (RelativeLayout) findViewById(R.id.shake_title_bar);    mDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);  mDrawerBtn = (Button) findViewById(R.id.handle);  mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener()  { public void onDrawerOpened()   {   mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_down));   TranslateAnimation titleup = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f);   titleup.setDuration(200);   titleup.setFillAfter(true);   mTitle.startAnimation(titleup);   }  });   /* 設定SlidingDrawer被關閉的事件處理 */  mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener()  { public void onDrawerClosed()   {   mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_up));   TranslateAnimation titledn = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f,Animation.RELATIVE_TO_SELF,0f);   titledn.setDuration(200);   titledn.setFillAfter(false);   mTitle.startAnimation(titledn);   }  });  loadSound() ;  mShakeListener = new ShakeListener(this);  mShakeListener.setOnShakeListener(new OnShakeListener() {   public void onShake() {   //Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到在同一時刻搖一搖的人。/n再試一次吧!", Toast.LENGTH_SHORT).show();   startAnim(); //開始 搖一搖手掌動畫   mShakeListener.stop();   sndPool.play(soundPoolMap.get(0), (float) 1, (float) 1, 0, 0,(float) 1.2);   new Handler().postDelayed(new Runnable(){    public void run(){    //Toast.makeText(getApplicationContext(), "抱歉,暫時沒有找到/n在同一時刻搖一搖的人。/n再試一次吧!", 500).setGravity(Gravity.CENTER,0,0).show();    sndPool.play(soundPoolMap.get(1), (float) 1, (float) 1, 0, 0,(float) 1.0);    Toast mtoast;    mtoast = Toast.makeText(getApplicationContext(),      "抱歉,暫時沒有找到/n在同一時刻搖一搖的人。/n再試一次吧!", 10);     //mtoast.setGravity(Gravity.CENTER, 0, 0);     mtoast.show();     mVibrator.cancel();     mShakeListener.start();         }   }, 2000);   }  });  }     private void loadSound() {   sndPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);  new Thread() {   public void run() {   try {    soundPoolMap.put(     0,     sndPool.load(getAssets().openFd(       "sound/shake_sound_male.mp3"), 1));     soundPoolMap.put(     1,     sndPool.load(getAssets().openFd(       "sound/shake_match.mp3"), 1));   } catch (IOException e) {    e.printStackTrace();   }   }  }.start();  }   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);  }  public void startVibrato(){ //定義震動  mVibrator.vibrate( new long[]{500,200,500,200}, -1); //第一個{}里面是節奏數組, 第二個參數是重復次數,-1為不重復,非-1俄日從pattern的指定下標開始重復  }   public void shake_activity_back(View v) { //標題欄 返回按鈕  this.finish();  }  public void linshi(View v) { //標題欄  startAnim();  }  @Override  protected void onDestroy() {  super.onDestroy();  if (mShakeListener != null) {   mShakeListener.stop();  }  } } 

 第二:一個檢測手機搖晃的監聽器類ShakeListener ,代碼如下:

package com.android.shake;  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 = 2000;  // 兩次檢測的時間間隔  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;  // 達到速度閥值,發出提示  if (speed >= SPEED_SHRESHOLD) {   onShakeListener.onShake();  }  }   public void onAccuracyChanged(Sensor sensor, int accuracy) {   }   // 搖晃監聽接口  public interface OnShakeListener {  public void onShake();  }  } 

代碼:

package com.android.shake;  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 = 2000;  // 兩次檢測的時間間隔  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;  // 達到速度閥值,發出提示  if (speed >= SPEED_SHRESHOLD) {   onShakeListener.onShake();  }  }   public void onAccuracyChanged(Sensor sensor, int accuracy) {   }   // 搖晃監聽接口  public interface OnShakeListener {  public void onShake();  }  } 

 第三:必須在AndroidManifest.xml中添加權限,否側搖動時不能監聽

<uses-permission android:name="android.hardware.sensor.accelerometer"/>

第四:效果圖:

android,微信搖一搖,重力感應

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清新县| 柘荣县| 平谷区| 绵阳市| 拜城县| 宁陵县| 河曲县| 贵溪市| 黔西| 阿鲁科尔沁旗| 昌乐县| 天台县| 长治市| 林西县| 罗城| 南城县| 宜兰市| 长丰县| 古蔺县| 富锦市| 吴江市| 克什克腾旗| 多伦县| 逊克县| 清河县| 瑞安市| 胶南市| 正宁县| 新乡市| 扎囊县| 宜丰县| 灵宝市| 通化县| 新野县| 银川市| 新民市| 银川市| 宁远县| 甘孜县| 潼南县| 卫辉市|