首先是model层
package com.example.wangxingqi.wangxingqi20180528.model;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
/**
* Created by AchillesL on 2016/11/15.
*/
public class MusicData implements Serializable{
/*音乐资源id*/
private int mMusicRes;
/*专辑图片id*/
private int mMusicPicRes;
/*音乐名称*/
private String mMusicName;
/*作者*/
private String mMusicAuthor;
public MusicData(int mMusicRes, int mMusicPicRes, String mMusicName, String mMusicAuthor) {
this.mMusicRes = mMusicRes;
this.mMusicPicRes = mMusicPicRes;
this.mMusicName = mMusicName;
this.mMusicAuthor = mMusicAuthor;
}
public int getMusicRes() {
return mMusicRes;
}
public int getMusicPicRes() {
return mMusicPicRes;
}
public String getMusicName() {
return mMusicName;
}
return mMusicAuthor;
}
}
service
package com.example.wangxingqi.wangxingqi20180528.service;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.example.wangxingqi.wangxingqi20180528.MainActivity;
import com.example.wangxingqi.wangxingqi20180528.model.MusicData;
import java.util.ArrayList;
import java.util.List;
/**
* Created by AchillesL on 2016/11/18.
*/
public class MusicService extends Service implements MediaPlayer.OnCompletionListener {
/*操作指令*/
public static final String ACTION_OPT_MUSIC_PLAY = "ACTION_OPT_MUSIC_PLAY";
public static final String ACTION_OPT_MUSIC_PAUSE = "ACTION_OPT_MUSIC_PAUSE";
public static final String ACTION_OPT_MUSIC_NEXT = "ACTION_OPT_MUSIC_NEXT";
public static final String ACTION_OPT_MUSIC_LAST = "ACTION_OPT_MUSIC_LAST";
public static final String ACTION_OPT_MUSIC_SEEK_TO = "ACTION_OPT_MUSIC_SEEK_TO";
/*状态指令*/
public static final String ACTION_STATUS_MUSIC_PLAY = "ACTION_STATUS_MUSIC_PLAY";
public static final String ACTION_STATUS_MUSIC_PAUSE = "ACTION_STATUS_MUSIC_PAUSE";
public static final String ACTION_STATUS_MUSIC_COMPLETE = "ACTION_STATUS_MUSIC_COMPLETE";
public static final String ACTION_STATUS_MUSIC_DURATION = "ACTION_STATUS_MUSIC_DURATION";
public static final String PARAM_MUSIC_DURATION = "PARAM_MUSIC_DURATION";
public static final String PARAM_MUSIC_SEEK_TO = "PARAM_MUSIC_SEEK_TO";
public static final String PARAM_MUSIC_CURRENT_POSITION = "PARAM_MUSIC_CURRENT_POSITION";
public static final String PARAM_MUSIC_IS_OVER = "PARAM_MUSIC_IS_OVER";
private int mCurrentMusicIndex = 0;
private boolean mIsMusicPause = false;
private List<MusicData> mMusicDatas = new ArrayList<>();
private MusicReceiver mMusicReceiver = new MusicReceiver();
private MediaPlayer mMediaPlayer = new MediaPlayer();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initMusicDatas(intent);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
initBoardCastReceiver();
}
private void initMusicDatas(Intent intent) {
if (intent == null) return;
List<MusicData> musicDatas = (List<MusicData>) intent.getSerializableExtra(MainActivity.PARAM_MUSIC_LIST);
mMusicDatas.addAll(musicDatas);
}
private void initBoardCastReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_OPT_MUSIC_PLAY);
intentFilter.addAction(ACTION_OPT_MUSIC_PAUSE);
intentFilter.addAction(ACTION_OPT_MUSIC_NEXT);
intentFilter.addAction(ACTION_OPT_MUSIC_LAST);
intentFilter.addAction(ACTION_OPT_MUSIC_SEEK_TO);
LocalBroadcastManager.getInstance(this).registerReceiver(mMusicReceiver,intentFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
mMediaPlayer.release();
mMediaPlayer = null;
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMusicReceiver);
}
private void play(final int index) {
if (index >= mMusicDatas.size()) return;
if (mCurrentMusicIndex == index && mIsMusicPause) {
mMediaPlayer.start();
} else {
mMediaPlayer.stop();
mMediaPlayer = null;
mMediaPlayer = MediaPlayer.create(getApplicationContext(), mMusicDatas.get(index)
.getMusicRes());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(this);
mCurrentMusicIndex = index;
mIsMusicPause = false;
int duration = mMediaPlayer.getDuration();
sendMusicDurationBroadCast(duration);
}
sendMusicStatusBroadCast(ACTION_STATUS_MUSIC_PLAY);
}
private void pause() {
mMediaPlayer.pause();
mIsMusicPause = true;
sendMusicStatusBroadCast(ACTION_STATUS_MUSIC_PAUSE);
}
private void stop() {
mMediaPlayer.stop();
}
private void next() {
if (mCurrentMusicIndex + 1 < mMusicDatas.size()) {
play(mCurrentMusicIndex + 1);
} else {
stop();
}
}
private void last() {
if (mCurrentMusicIndex != 0) {
play(mCurrentMusicIndex - 1);
}
}
private void seekTo(Intent intent) {
if (mMediaPlayer.isPlaying()) {
int position = intent.getIntExtra(PARAM_MUSIC_SEEK_TO, 0);
mMediaPlayer.seekTo(position);
}
}
@Override
public void onCompletion(MediaPlayer mp) {
sendMusicCompleteBroadCast();
}
class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_OPT_MUSIC_PLAY)) {
play(mCurrentMusicIndex);
} else if (action.equals(ACTION_OPT_MUSIC_PAUSE)) {
pause();
} else if (action.equals(ACTION_OPT_MUSIC_LAST)) {
last();
} else if (action.equals(ACTION_OPT_MUSIC_NEXT)) {
next();
} else if (action.equals(ACTION_OPT_MUSIC_SEEK_TO)) {
seekTo(intent);
}
}
}
private void sendMusicCompleteBroadCast() {
Intent intent = new Intent(ACTION_STATUS_MUSIC_COMPLETE);
intent.putExtra(PARAM_MUSIC_IS_OVER, (mCurrentMusicIndex == mMusicDatas.size() - 1));
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void sendMusicDurationBroadCast(int duration) {
Intent intent = new Intent(ACTION_STATUS_MUSIC_DURATION);
intent.putExtra(PARAM_MUSIC_DURATION, duration);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void sendMusicStatusBroadCast(String action) {
Intent intent = new Intent(action);
if (action.equals(ACTION_STATUS_MUSIC_PLAY)) {
intent.putExtra(PARAM_MUSIC_CURRENT_POSITION,mMediaPlayer.getCurrentPosition());
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
utils
package com.example.wangxingqi.wangxingqi20180528.utils;
import android.content.Context;
/**
* Created by AchillesL on 2016/11/16.
*/
public class DisplayUtil {
/*手柄起始角度*/
public static final float ROTATION_INIT_NEEDLE = -30;
/*截图屏幕宽高*/
private static final float BASE_SCREEN_WIDTH = (float) 1080.0;
private static final float BASE_SCREEN_HEIGHT = (float) 1920.0;
/*唱针宽高、距离等比例*/
public static final float SCALE_NEEDLE_WIDTH = (float) (276.0 / BASE_SCREEN_WIDTH);
public static final float SCALE_NEEDLE_MARGIN_LEFT = (float) (500.0 / BASE_SCREEN_WIDTH);
public static final float SCALE_NEEDLE_PIVOT_X = (float) (43.0 / BASE_SCREEN_WIDTH);
public static final float SCALE_NEEDLE_PIVOT_Y = (float) (43.0 / BASE_SCREEN_WIDTH);
public static final float SCALE_NEEDLE_HEIGHT = (float) (413.0 / BASE_SCREEN_HEIGHT);
public static final float SCALE_NEEDLE_MARGIN_TOP = (float) (43.0 / BASE_SCREEN_HEIGHT);
/*唱盘比例*/
public static final float SCALE_DISC_SIZE = (float) (813.0 / BASE_SCREEN_WIDTH);
public static final float SCALE_DISC_MARGIN_TOP = (float) (190 / BASE_SCREEN_HEIGHT);
/*专辑图片比例*/
public static final float SCALE_MUSIC_PIC_SIZE = (float) (533.0 / BASE_SCREEN_WIDTH);
/*设备屏幕宽度*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/*设备屏幕高度*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
}
package com.example.wangxingqi.wangxingqi20180528.utils;
import android.graphics.Bitmap;
/**
* Created by jay on 11/7/15.
*/
public class FastBlurUtil {
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
// Stack Blur v1.0 from
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
//
// Java Author: Mario Klingemann <mario at quasimondo.com>
// http://incubator.quasimondo.com
// created Feburary 29, 2004
// Android port : Yahel Bouaziz <yahel at kayenko.com>
// http://www.kayenko.com
// ported april 5th, 2012
// This is a compromise between Gaussian Blur and Box blur
// It creates much better looking blurs than Box Blur, but is
// 7x faster than my Gaussian Blur implementation.
//
// I called it Stack Blur because this describes best how this
// filter works internally: it creates a kind of moving stack
// of colors whilst scanning through the image. Thereby it
// just has to add one new block of color to the right side
// of the stack and remove the leftmost color. The remaining
// colors on the topmost layer of the stack are either added on
// or reduced by one, depending on if they are on the right or
// on the left side of the stack.
//
// If you are using this algorithm in your code please add
// the following line:
//
// Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
Bitmap bitmap;
if (canReuseInBitmap) {
bitmap = sentBitmap;
} else {
bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
}
if (radius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
bitmap.getPix