Android 微信摇一摇播放音乐简单功能实现

在这里需要设置一个权限:
uses-permission android:name=“android.permission.VIBRATE”
然后来看一下,mainactivity.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--摇一摇中心图片-->
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:textColor="@color/colorRed"
        android:text="@string/app_c"/>
    <LinearLayout
        android:gravity="center"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <!--顶部的横线和图片-->
        <LinearLayout
            android:gravity="center_horizontal|bottom"
            android:id="@+id/main_linear_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <ImageView
                android:src="@mipmap/top"
                android:id="@+id/main_shake_top"
                android:layout_width="wrap_content"
                android:layout_height="100dp"/>
            <ImageView
                android:background="@mipmap/line"
                android:id="@+id/main_shake_top_line"
                android:layout_width="match_parent"
                android:layout_height="5dp"/>
        </LinearLayout>

        <!--底部的横线和图片-->
        <LinearLayout
            android:gravity="center_horizontal|bottom"
            android:id="@+id/main_linear_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:background="@mipmap/line"
                android:id="@+id/main_shake_bottom_line"
                android:layout_width="match_parent"
                android:layout_height="1dp"/>
            <ImageView
                android:src="@mipmap/bottom"
                android:id="@+id/main_shake_bottom"
                android:layout_width="wrap_content"
                android:layout_height="100dp"/>

        </LinearLayout>
    </LinearLayout>
    <Button
        android:id="@+id/but"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你点我"/>
</RelativeLayout>

mipmap-hdpi文件夹下需要放入maintivity.xml相对应的图片,这里就不多演示了

因为需要播放音乐所以这里需要建一个raw文件夹,这里面是放音乐资源的,也不过多演示了
values文件夹下的colors.ml与Strings.xml分别要写的代码:
colors.xml是设置颜色用的
在这里插入图片描述
String.xml可以放入字符串:
在这里插入图片描述
接下来就是最为重要的mainactivity里面的代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
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.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
private AlertDialog.Builder dialog;
private static final String TAG = “MainActivity”;
private static final int START_SHAKE = 0x1;
private static final int AGAIN_SHAKE = 0x2;
private static final int END_SHAKE = 0x3;

private SensorManager mSensorManager;
private Sensor mAccelerometerSensor;
private Vibrator mVibrator;//手机震动
private SoundPool mSoundPool;//摇一摇音效

//记录摇动状态
private boolean isShake = false;


private LinearLayout mTopLayout;
private LinearLayout mBottomLayout;
private ImageView mTopLine;
private ImageView mBottomLine;
private  TextView mTextView;

private MyHandler mHandler;
private int mWeiChatAudio;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //设置只竖屏
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_main);
    //初始化View
    initView();
    mHandler = new MyHandler(this);

    //初始化SoundPool
    mSoundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5);
    mWeiChatAudio = mSoundPool.load(this, R.raw.mus, 1);

    //获取Vibrator震动服务
    mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);


}

private void initView() {
    mTopLayout = (LinearLayout) findViewById(R.id.main_linear_top);
    mBottomLayout = ((LinearLayout) findViewById(R.id.main_linear_bottom));
    mTopLine = (ImageView) findViewById(R.id.main_shake_top_line);
    mBottomLine = (ImageView) findViewById(R.id.main_shake_bottom_line);
    mTextView = (TextView) findViewById(R.id.tv);
    findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });


    //默认
    mTopLine.setVisibility(View.GONE);
    mBottomLine.setVisibility(View.GONE);


}

@Override
protected void onStart() {
    super.onStart();
    //获取 SensorManager 负责管理传感器
    mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));
    if (mSensorManager != null) {
        //获取加速度传感器
        mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if (mAccelerometerSensor != null) {
            mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_UI);
        }
    }
}

@Override
protected void onPause() {
    // 务必要在pause中注销 mSensorManager
    // 否则会造成界面退出后摇一摇依旧生效的bug
    if (mSensorManager != null) {
        mSensorManager.unregisterListener(this);
    }
    super.onPause();
}

///////////////////////////////////////////////////////////////////////////
// SensorEventListener回调方法
///////////////////////////////////////////////////////////////////////////
@Override
public void onSensorChanged(SensorEvent event) {
    int type = event.sensor.getType();

    if (type == Sensor.TYPE_ACCELEROMETER) {
        //获取三个方向值
        float[] values = event.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];
        Log.i(TAG, "onSensorChanged:==x "+x);
        Log.i(TAG, "onSensorChanged: ==z"+z);
        Log.i(TAG, "onSensorChanged: ==y"+y);




        if ((Math.abs(x) > 17 || Math.abs(y) > 17 || Math
                .abs(z) > 17) && !isShake) {
            isShake = true;
            // TODO: 2016/10/19 实现摇动逻辑, 摇动后进行震动
            Thread thread = new Thread() {
                @Override
                public void run() {


                    super.run();
                    try {
                        Log.d(TAG, "onSensorChanged: 摇动");

                        //开始震动 发出提示音 展示动画效果
                        mHandler.obtainMessage(START_SHAKE).sendToTarget();
                        Thread.sleep(500);
                        //再来一次震动提示
                        mHandler.obtainMessage(AGAIN_SHAKE).sendToTarget();
                        Thread.sleep(500);
                        mHandler.obtainMessage(END_SHAKE).sendToTarget();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
        }
    }
}

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


private static class MyHandler extends Handler {
    private WeakReference<MainActivity> mReference;
    private MainActivity mActivity;
    public MyHandler(MainActivity activity) {
        mReference = new WeakReference<MainActivity>(activity);
        if (mReference != null) {
            mActivity = mReference.get();
        }
    }
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case START_SHAKE:
                //This method requires the caller to hold the permission VIBRATE.
                mActivity.mVibrator.vibrate(300);
                //发出提示音
                mActivity.mSoundPool.play(mActivity.mWeiChatAudio, 1, 1, 0, 0, 1);
                mActivity.mTopLine.setVisibility(View.VISIBLE);
                mActivity.mBottomLine.setVisibility(View.VISIBLE);
                mActivity.startAnimation(false);//参数含义: (不是回来) 也就是说两张图片分散开的动画

                break;
            case AGAIN_SHAKE:
                mActivity.mTextView.setText(R.string.app_b);
                mActivity.mVibrator.vibrate(300);
                break;
            case END_SHAKE:
                //整体效果结束, 将震动设置为false
                mActivity.mTextView.setText(R.string.app_c);
                mActivity.isShake = false;
                // 展示上下两种图片回来的效果
                mActivity.startAnimation(true);

                break;
        }
    }
}

/**
 * 开启 摇一摇动画
 *
 * @param isBack 是否是返回初识状态
 */
private void startAnimation(boolean isBack) {
    //动画坐标移动的位置的类型是相对自己的
    int type = Animation.RELATIVE_TO_SELF;

    float topFromY;
    float topToY;
    float bottomFromY;
    float bottomToY;
    if (isBack) {
        topFromY = -0.5f;
        topToY = 0;
        bottomFromY = 0.5f;
        bottomToY = 0;
    } else {
        topFromY = 0;
        topToY = -0.5f;
        bottomFromY = 0;
        bottomToY = 0.5f;
    }

    //上面图片的动画效果
    TranslateAnimation topAnim = new TranslateAnimation(
            type, 0, type, 0, type, topFromY, type, topToY
    );
    topAnim.setDuration(200);
    //动画终止时停留在最后一帧~不然会回到没有执行之前的状态
    topAnim.setFillAfter(true);

    //底部的动画效果
    TranslateAnimation bottomAnim = new TranslateAnimation(
            type, 0, type, 0, type, bottomFromY, type, bottomToY
    );
    bottomAnim.setDuration(200);
    bottomAnim.setFillAfter(true);

    //大家一定不要忘记, 当要回来时, 我们中间的两根线需要GONE掉
    if (isBack) {
        bottomAnim.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {}
            @Override
            public void onAnimationRepeat(Animation animation) {}
            @Override
            public void onAnimationEnd(Animation animation) {
                //当动画结束后 , 将中间两条线GONE掉, 不让其占位
                mTopLine.setVisibility(View.GONE);
                mBottomLine.setVisibility(View.GONE);
                dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setMessage("爱本无罪,你说对吗?");
                dialog.setTitle("你还爱我吗?");
                dialog.setPositiveButton("爱", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //获取String.xml里面的值
                        String mystring = getResources().getString(R.string.app_b);
                        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                        intent.putExtra("gy",mystring);
                        startActivity(intent);
                        Toast.makeText(MainActivity.this,""+mystring,Toast.LENGTH_LONG).show();
                    }
                });
                dialog.setNegativeButton("不爱",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"狗子你是不是变了!",Toast.LENGTH_LONG).show();
                    }
                });
                dialog.show();
            }
        });
    }
    //设置动画
    mTopLayout.startAnimation(topAnim);
    mBottomLayout.startAnimation(bottomAnim);

}

}

到这里就完成了摇一摇功能,希望能够帮助到你。。。。

猜你喜欢

转载自blog.csdn.net/gy1115/article/details/84821576