Android Studio学习路程(13)

今天在网上学习了几个新的知识点,一个是传感器,一个是添加音效池,一个是实现震动的效果。

首先,安卓有很多自带的传感器,有加速度传感器等等,

private SensorManager sensorManager;private Sensor sensor;...sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);这个是安卓官方文件给出的一个例子,添加音效池和添加震动的方法是差不多的;下面是一个实现微信摇一摇功能的APP的代码:
package com.example.hp.wechat_shake;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    private Button mButton;

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

        //初始化控件
        initUI();

    }
    private void initUI() {
        mButton = (Button) findViewById(R.id.btn_shake);
        findViewById(R.id.btn_shake).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch(view.getId()){
            case R.id.btn_shake:
                intent.setClass(this,ShakeActivity.class);
                break;
        }
        startActivity(intent);
    }
}

MainActivity

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.wechat_shake.MainActivity">

   <Button
       android:id="@+id/btn_shake"
       android:text="微信摇一摇"
       android:textSize="30dp"
       android:layout_width="match_parent"
       android:layout_height="65dp"/>
</RelativeLayout>

MainActivity_XML

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    tools:context="com.example.hp.wechat_shake.ShakeActivity">

    <ImageView
        android:id="@+id/iv_shake_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shakehideimg_man2"
        android:layout_centerInParent="true"/>

    <LinearLayout
        android:id="@+id/id_shake_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:src="@drawable/shake_logo_up"
            android:id="@+id/iv_shake_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:src="@drawable/shake_logo_down"
            android:id="@+id/iv_shake_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</RelativeLayout>

ShakeActivity_XML

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.hp.wechat_shake"
          xmlns:android="http://schemas.android.com/apk/res/android">
    /*******振动器使用权限**********/
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".ShakeActivity">
        </activity>
    </application>

</manifest>

manifest_XML

package com.example.hp.wechat_shake;

import android.content.Context;
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.Bundle;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class ShakeActivity extends ActionBarActivity implements SensorEventListener{

    private ImageView mShakeImg;
    private ImageView mShakeUp;
    private ImageView mShakeDown;
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private AnimationSet mSetUp;
    private AnimationSet mSetDown;
    private SoundPool mSoundPool;
    private int loadId;
    private Vibrator mVibrator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shake);
        //初始化控件
        initUI();
        //初始化动画
        initAnimation();
        //初始化音效池
        initSoundPool();
        //初始化振动器
        initVibrator();

    }

    private void initVibrator() {
        //获取振动器管理者对象
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }

    private void initSoundPool() {
        //获取音效池对象
        mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);//2代表音效池最多下载两个播放器,中间的参数代表播放器的类型,最后一个0代表源文件的一个优先级
        //加载音效到音效池当中
        loadId=mSoundPool.load(this,R.raw.awe,1);

    }

    private void initUI() {
        mShakeImg = (ImageView) findViewById(R.id.iv_shake_img);
        mShakeUp = (ImageView) findViewById(R.id.iv_shake_up);
        mShakeDown = (ImageView) findViewById(R.id.iv_shake_down);
        //1.获取传感器管理者对象
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //2.获取指定的传感器对象---加速度传感器
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        //3.注册传感器对象
        mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_NORMAL);

    }

    @Override
    protected void onDestroy() {
        //4.注销传感器对象
        super.onDestroy();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        /*当传感器的数值发生改变时会调用的方法(函数)*/
        float[] values = sensorEvent.values;
        float x = values[0];
        float y = values[1];
        float z = values[3];
        int minValue = 12;
        if(Math.abs(x)>minValue||Math.abs(y)>minValue||Math.abs(z)>minValue){
            //开始震动
            long[]patten = {300,500};
            mVibrator.vibrate(patten,-1);

            //开始播放音效
            mSoundPool.play(loadId,1,1,1,0,1);


            //开始动画效果
            mShakeUp.startAnimation(mSetUp);
            mShakeDown.startAnimation(mSetDown);

        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
        /*当传感器的精度发生改变时会调用的方法*/
    }

    private void initAnimation(){
        //上面图片对应的动画效果
        TranslateAnimation mAnimationUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1);
        mAnimationUp.setDuration(500);//设置上面的图片向上的时间为5秒
        TranslateAnimation mAnimationUpDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0);
        mAnimationUpDown.setDuration(500);
        mSetUp = new AnimationSet(true);
        //将上面的一张图片的上下移动的动画效果放到一个集合里
        mSetUp.addAnimation(mAnimationUp);
        mSetUp.addAnimation(mAnimationUpDown);
        //设置动画之间的执行时间差
        mSetUp.setStartOffset(500);

        TranslateAnimation mAnimationDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1);
        mAnimationDown.setDuration(500);//设置上面的图片向上的时间为5秒
        TranslateAnimation mAnimationDownUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF,0);
        mAnimationDownUp.setDuration(500);
        mSetDown = new AnimationSet(true);
        //将下面的一张图片的上下移动的动画效果放到一个集合里
        mSetDown.addAnimation(mAnimationDown);
        mSetDown.addAnimation(mAnimationDownUp);
        //设置动画之间的执行时间差
        mSetDown.setStartOffset(500);

    }
}

ShakeActivity

 

相关推荐