欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 如何实现微信摇一摇(加速度传感器)

程序员文章站 2022-05-29 21:09:35
...

使用加速度传感器判断手机是否在摇晃

展示代码:

MainActivity:

​
package com.example.day09yaoyiyao;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private ImageView image_id;
    private ImageView image_up;
    private ImageView image_down;
    private SoundPool soundPool;
    private int mLoadId;

    @SuppressLint("ServiceCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        mLoadId = soundPool.load(getActivity(),R.raw.awe,1);

        //加速度传感器
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[0];//x轴的加速度
            float y = event.values[1];//y轴的加速度
            float z = event.values[2];//z轴的加速度

            //静止状态下加速度为9.8,当加速度>15的时候就表示在摇晃手机
            if (x > 15 || y > 15 || z > 15) {

                Toast.makeText(this, "你正在摇一摇", Toast.LENGTH_SHORT).show();

                //播放音乐
                //声音id, 左声道(0.0f -- 1.0f) , 右声道, 优先级, 是否循环(0 不循环, 1 循环) , 播放比例( 正常播放 1)
                soundPool.play(mLoadId,1.0f,1.0f,1,0,1.0f);



                //设置动画的效果
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_up);
                image_up.startAnimation(animation);
                Animation animation1 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_down);
                image_down.startAnimation(animation1);
            }

        }
    }

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

    }

    private void initView() {
        image_id = (ImageView) findViewById(R.id.image_id);
        image_up = (ImageView) findViewById(R.id.image_up);
        image_down = (ImageView) findViewById(R.id.image_down);
    }
}

​

布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainActivity">

    <ImageView
        android:id="@+id/image_id"
        android:background="@drawable/shakehideimg_man2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/image_up"
            android:layout_weight="1"
            android:src="@drawable/shake_logo_up"
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="0dp" />
        <ImageView
            android:id="@+id/image_down"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:src="@drawable/shake_logo_down"
            android:layout_width="match_parent"
            android:layout_height="0dp" />
    </LinearLayout>



</FrameLayout>

动画(up):

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:toYDelta="-300"
    android:toXDelta="0"
    android:fromYDelta="0"
    android:fromXDelta="0" >

</translate>

down:

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:toYDelta="300"
    android:toXDelta="0"
    android:fromYDelta="0"
    android:fromXDelta="0" >

</translate>

Android 如何实现微信摇一摇(加速度传感器)