<ImageView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/flower"/><LinearLayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"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>
Public classMainactivityextendsappcompatactivity {PrivateImageView up; PrivateImageView down; //the last time you shook your phone Private LongLasttime; PrivateSoundpool Soundpool; Private intSound1; PrivateVibrator Vibrator; PrivateSensoreventlistener listener =NewSensoreventlistener () {//called when the acceleration of the phone is changed@Override Public voidonsensorchanged (Sensorevent event) {//get the change in the speed at which the phone is added in different directions floatValuesx = Math.Abs (event.values[0]); floatValuesy = Math.Abs (event.values[1]); floatValuesz = Math.Abs (event.values[2]); if(Valuesx > | | valuesy > | | Valuesz > 17) {startanimation (); PlaySound (); }} @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) { } }; Private voidPlaySound () {//1. The ID of the sound//2.3. Indicates the volume of the left and right channels//4. Priority Level//5. Whether to cycle//6. Sound Playback rateSoundpool.play (Sound1, 1, 1, 0, 0, 1); //Mobile phone Vibration//1. The rhythm of the vibration off/on/off/on/off/on ...//2. Indicates whether the vibration is repeated, 1 means no repetitionVibrator.vibrate (New Long[]{100, 200, 100, 200, 100, 200}, 1); } Private voidstartanimation () {//if the phone is shaking two times less than 1 seconds, only one animation is performed LongCurrenttimemillis =System.currenttimemillis (); if(Currenttimemillis-lasttime < 1000) { return; } lasttime=Currenttimemillis; Animationset upset=NewAnimationset (true); Translateanimation Upup=NewTranslateanimation (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=NewTranslateanimation (translateanimation.relative_to_self, 0, Translateanimation.relative_to_self,0, translateanimation.relative_to_self, 0, translateanimation.relative_to_self, 1); Updown.setduration (1000); Updown.setstartoffset (1000); Upset.addanimation (Upup); Upset.addanimation (UpDown); Up.startanimation (upset); Animationset Downset=NewAnimationset (true); Translateanimation Downup=NewTranslateanimation (translateanimation.relative_to_self, 0, Translateanimation.relative_to_self,0, translateanimation.relative_to_self, 0, translateanimation.relative_to_self, 1); Downup.setduration (1000); Translateanimation Downdown=NewTranslateanimation (translateanimation.relative_to_self, 0, Translateanimation.relative_to_self,0, translateanimation.relative_to_self, 0, translateanimation.relative_to_self, 1); Downdown.setduration (1000); Downdown.setstartoffset (1000); Downset.addanimation (Downup); Downset.addanimation (Downdown); Down.startanimation (Downset); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); up=( (ImageView) Findviewbyid (r.id.up)); down=( (ImageView) Findviewbyid (R.id.down)); Initsensor (); Initsoundpool (); //Get mobile phone Shake serviceVibrator =(Vibrator) Getsystemservice (Vibrator_service); } /*** Initialize the sound pool*/ Private voidInitsoundpool () {if(Build.VERSION.SDK_INT > 20) {Soundpool.builder Builder=NewSoundpool.builder (); //1. Maximum number of concurrent streamsBuilder.setmaxstreams (3); Audioattributes.builder Aabuilder=NewAudioattributes.builder (); Aabuilder.setlegacystreamtype (Audiomanager.stream_music); Builder.setaudioattributes (Aabuilder.build ()); Soundpool=Builder.build (); } Else{Soundpool=NewSoundpool (3, Audiomanager.stream_music, 0); } //loading an audio fileSound1 = Soundpool.load ( This, R.raw.awe, 1); } /*** Initialize sensor*/ Private voidinitsensor () {//get to a sensor managerSensormanager Sensormanager =(Sensormanager) Getsystemservice (Sensor_service); //get an accelerometerSENSOR sensor =sensormanager.getdefaultsensor (Sensor.type_accelerometer); //Register sensor monitoring,//1. Listener//2. Acceleration Sensor//3. Sensor Sensitivity//sensor sensitivity is divided into four levels, from the top down to lower sensitivity//sensor_delay_fastest//Sensor_delay_game//sensor_delay_ui//Sensor_delay_normalSensormanager.registerlistener (Listener, sensor, sensormanager.sensor_delay_game); }}
Sensor Sensors (SHAKE)