The shake function is to use the mobile phone acceleration sensor to determine whether it is in a shake state and perform corresponding operations.
1. Put the music file under res/raw. If there is no raw, create a
2. Layout files
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_shake" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.sq.dissertation.activity.ShakeActivity" android:background="#1d1d1d"><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/shakehideimg_man2"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/hand_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/shake_logo_up"/> <ImageView android:id="@+id/hand_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/shake_logo_down"/> </LinearLayout></RelativeLayout>
3. Java code
public class ShakeActivity extends AppCompatActivity implements SensorEventListener { private ImageView ivUp; private ImageView ivDown; private SensorManager sensorManager; private Vibrator vibrator; private Sensor sensor; private MediaPlayer player; private ObjectAnimator upAnimator; private ObjectAnimator downAnimator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shake); ivUp = ((ImageView) findViewById(R.id.hand_up)); ivDown = ((ImageView) findViewById(R.id.hand_down)); //Get SensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE)); //Instantiate the object vibrator of the mobile phone to vibrator = ((Vibrator) getSystemService(VIBRATOR_SERVICE)); //Get the acceleration sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); /*Instantiated object, parameters: the maximum number of streams played simultaneously, that is, the upper limit of the number of music played simultaneously; the type of streams is generally used to use AudioManager.STREAM_MUSIC to indicate the sampling rate conversion quality that can be repeated, but this function cannot take effect yet, it is recommended to use 0 */// soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); // Parameter three has no effect, it is recommended to use 1// loadId = soundPool.load(ShakeActivity.this, R.raw.three, 1); player = MediaPlayer.create(this, R.raw.music); initAnimation(); } private void initAnimation() { //Animation of the upper and lower two pictures upAnimator = ObjectAnimator.ofFloat(ivUp, "translationY", 0, -200, 0); upAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); upAnimator.setDuration(2000); downAnimator = ObjectAnimator.ofFloat(ivDown, "translationY", 0, 200, 0); downAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); downAnimator.setDuration(2000); } @Override protected void onResume() { super.onResume(); if (sensorManager != null) { //Register listener, monitor, sensor, obtain sensor frequency sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); } } @Override protected void onPause() { super.onPause(); if (sensorManager != null) { //Cancel the listener sensorManager.unregisterListener(this); //Release audio resource// soundPool.unload(loadId); } } @Override protected void onDestroy() { super.onDestroy(); player.release(); } @Override public void onSensorChanged(SensorEvent event) { //Get data when sensor information changes float[] values = event.values; //Graining acceleration in the x-axis direction, positive float x = values[0]; //The gravity acceleration in the y-axis direction is positive float y = values[1]; //The gravity acceleration in the z-axis direction is positive float z = values[2]; //The general gravity acceleration in these three directions reaches 40 and reaches the state of shaking the phone int value = 18; if (Math.abs(x) > value || Math.abs(y) > value || Math.abs(z) > 19) { long patter[] = {200, 1000}; vibrator.vibrate(patter,1); upAnimator.start(); downAnimator.start(); /* 1f: left channel volume 1f: right channel volume 1: audio priority, the higher the value, the higher the priority, 0: the number of loop playback times, 0 is playback once, -1 is infinite loop, other values are loop+1 times, 1f: playback rate, range 0.5-2.0 (1 is normal rate) */// soundPool.play(loadId, 1f, 1f, 1, 0, 1f); if (player.isPlaying()) { player.seekTo(0); }else{ player.start(); } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { }}Supplement: SoundPool appears in Java code to play sound effects. It is suitable for playing short sound effects. It can only apply for a maximum of 1M memory space. Some files are not large in themselves, but they will be large after decoding, so generally, they are not played with song files.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.