传感器的使用,高仿微信摇一摇,动画加声音
生活随笔
收集整理的這篇文章主要介紹了
传感器的使用,高仿微信摇一摇,动画加声音
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很多時候我們的應用需要使用傳感器,使手機應用更加方便和可玩性更高, Google為我們提供了十一種傳感器
#define SENSOR_TYPE_ACCELEROMETER 1 //加速度#define SENSOR_TYPE_MAGNETIC_FIELD 2 //磁力#define SENSOR_TYPE_ORIENTATION 3 //方向#define SENSOR_TYPE_GYROSCOPE 4 //陀螺儀#define SENSOR_TYPE_LIGHT 5 //光線感應#define SENSOR_TYPE_PRESSURE 6 //壓力#define SENSOR_TYPE_TEMPERATURE 7 //溫度#define SENSOR_TYPE_PROXIMITY 8 //接近#define SENSOR_TYPE_GRAVITY 9 //重力#define SENSOR_TYPE_LINEAR_ACCELERATION 10//線性加速度#define SENSOR_TYPE_ROTATION_VECTOR 11//旋轉矢量今天我們就實現微信中的搖一搖功能,分析一波,首先是直觀的感受是,搖一搖的時候,手機振動了,其次是有動畫效果,當然也少不了聲音的效果。
xml布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#1f1f1f"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@drawable/flower" /><LinearLayout android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerInParent="true"android:gravity="center"android:orientation="vertical"><ImageView android:id="@+id/up"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/up" /><ImageView android:id="@+id/down"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/down" /></LinearLayout> </RelativeLayout>因為我們需要搖一搖的時候有音效,所以需要在在res中新建一個raw文件夾,然后把音頻文件放進文件夾中, 我的音頻文件名為awe
邏輯的處理
public class MainActivity extends AppCompatActivity{//記錄上一次晃動手機的時間private long lastTime;private ImageView upIv;private ImageView downIv;private int sound1;private SoundPool soundPool;private Vibrator vibrator;private SensorManager sensorManager;@Overrideprotected void onCreate (Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//振動器vibrator = ((Vibrator) getSystemService(VIBRATOR_SERVICE));//傳感器sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);upIv = ((ImageView) findViewById(R.id.up));downIv = ((ImageView) findViewById(R.id.down));initSoundPool();}@Overrideprotected void onResume (){super.onResume();if(sensorManager != null){// 注冊監聽器// 第一個參數是Listener,第二個參數是所得傳感器類型,第三個參數值獲取傳感器信息的頻率sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);}}//記得在不用的時候關掉傳感器,因為手機黑屏是不會自動關掉傳感器的,當然如果你覺得電量一直都很足,那算我多嘴咯。@Overrideprotected void onStop (){super.onStop();if(sensorManager != null){// 取消監聽器sensorManager.unregisterListener(sensorEventListener);}}private SensorEventListener sensorEventListener = new SensorEventListener() {//當加速度發生改變時調用//當加速度放生變化時就會調用該方法,所以在一次晃動中該方法實際會調用多次@Overridepublic void onSensorChanged(SensorEvent event) {//獲取手機在不同方向上加速度的大小float valueX = Math.abs(event.values[0]);float valueY = Math.abs(event.values[1]);float valueZ = Math.abs(event.values[2]);//當手機在任意一個方向上加速度的大小超過17時,認為晃動手機了if (valueX > 17 || valueY > 17 || valueZ > 17) {//獲取當前毫秒數long currentTimeMillis = System.currentTimeMillis();//如果兩次連續晃動的時間小于1秒if (currentTimeMillis - lastTime < 1000) {return;}lastTime = currentTimeMillis;//1.執行動畫playAnimation();//2.播放音效playSound();//3.手機震動//1.震動節奏,off/on/off/on,// new long[]{100, 200, 100, 200, 100, 200}這個數組表示// 停止震動100ms/震動200ms/停止震動100ms/......//2.是否循環,-1表示不循環vibrator.vibrate(new long[]{100, 200, 100, 200, 100, 200}, -1);}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}};//執行動畫private void playAnimation() {AnimationSet up = new AnimationSet(false);TranslateAnimation upUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF,0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,TranslateAnimation.RELATIVE_TO_SELF, -1);upUp.setDuration(1000);TranslateAnimation upDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF,0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,TranslateAnimation.RELATIVE_TO_SELF, 1);upDown.setDuration(1000);//延遲1秒執行upDown.setStartOffset(1000);up.addAnimation(upUp);up.addAnimation(upDown);upIv.startAnimation(up);AnimationSet down = new AnimationSet(false);TranslateAnimation downDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF,0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,TranslateAnimation.RELATIVE_TO_SELF, 1);downDown.setDuration(1000);TranslateAnimation downUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF,0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,TranslateAnimation.RELATIVE_TO_SELF, -1);downUp.setDuration(1000);//延遲1秒執行downUp.setStartOffset(1000);down.addAnimation(downDown);down.addAnimation(downUp);downIv.startAnimation(down);}private void playSound() {//1.音頻文件id//2.3 表示左右聲道的音量//4.優先級//5.循環次數,-1表示無限循環//6.播放速率,取值為0.5~2之間,1表示正常速率播放soundPool.play(sound1, 1, 1, 1, 0, 1);}private void initSoundPool() {if (Build.VERSION.SDK_INT > 20) {SoundPool.Builder builder = new SoundPool.Builder();//設置最大并發流builder.setMaxStreams(3);AudioAttributes.Builder attributes = new AudioAttributes.Builder();attributes.setLegacyStreamType(AudioManager.STREAM_MUSIC);//設置音頻流builder.setAudioAttributes(attributes.build());soundPool = builder.build();} else {//1.最大并發流//2.音頻流//3.音頻質量soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);}//讀取音頻文件,返回值為音頻文件id//第三個參數表示音頻優先級sound1 = soundPool.load(this, R.raw.awe, 1);} }當然,需要申明權限,申明手機振動的權限。
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>總結
以上是生活随笔為你收集整理的传感器的使用,高仿微信摇一摇,动画加声音的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 21闭关修炼 解析分册
- 下一篇: opencv检测相交点_在网络摄像头fe