Android Studio实现微信摇一摇(传感器)

所需文件

Android Studio实现微信摇一摇(传感器)_第1张图片
Android Studio实现微信摇一摇(传感器)_第2张图片
Android Studio实现微信摇一摇(传感器)_第3张图片

代码实现

activiyty

package com.example.day9;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class WxActivity extends AppCompatActivity implements SensorEventListener {

    ImageView up;
    ImageView down;
    SensorManager sensorManager;
    Sensor sensor;
    Vibrator vibrator;
    SoundPool soundPool;
    TranslateAnimation translateAnimationUp;
    TranslateAnimation translateAnimationDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx);
        initView();
        initData();
    }

    private void initData() {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        soundPool.load(this, R.raw.wx, 1);
        translateAnimationUp = new TranslateAnimation(0, 0, 0, -50);
        translateAnimationDown = new TranslateAnimation(0, 0, 0, 50);
    }

    private void initView() {
        up = findViewById(R.id.up);
        down = findViewById(R.id.down);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, sensor, 200);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];

        if (Math.abs(x) > 15 | Math.abs(y) > 15 | Math.abs(z) > 15) {
            long[] patten = {300, 500};
            vibrator.vibrate(patten, -1);
            //声音效果
            soundPool.play(1, 1, 1, 1, 1, 1);
            //动画效果
            translateAnimationUp.setDuration(2000);
            translateAnimationDown.setDuration(2000);

            up.startAnimation(translateAnimationUp);
            down.startAnimation(translateAnimationDown);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WxActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/shakehideimg_man2"/>

    <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="@mipmap/shake_logo_up"/>

        <ImageView
            android:id="@+id/down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/shake_logo_down"/>
    LinearLayout>

RelativeLayout>

你可能感兴趣的:(Android Studio实现微信摇一摇(传感器))