音乐播放器动画的相关优化修改。
问题点:原始版本在onCreate时就立刻播放动画,无法灵活的停止音乐的动画效果。
更改方式:动画的更新需要独立封装,点击播放就进行动画展示,点击暂停就进行暂停。
旧代码:StoryViewController.class
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(10000);
rotateAnimation.setFillAfter(true);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setInterpolator(mContext, android.R.anim.linear_interpolator);//设置动画插入器
mRotate.startAnimation(rotateAnimation);//立即执行动画
}
});
新代码: StoryViewController.class
上面问题涉及到的知识点:自定义接口,当音乐状态更新时进行回调,更新UI. (接口的定义方式)
1: StoryViewController.class
注册:
playerManager.setmOnPlayerStatusListener(mStatusListener);
回调:
private IMediaPlayerStatusListener mStatusListener = new IMediaPlayerStatusListener() {
@Override
public void onPlay() {
mPlayState.setBackgroundResource(R.drawable.music_start_selector);
rotateAnimation.startNow();
}
@Override
public void onPause() {
mPlayState.setBackgroundResource(R.drawable.music_pause_selector);
rotateAnimation.cancel();
}
};
2: MediaPlayerManager.class
public void setmOnPlayerStatusListener(IMediaPlayerStatusListener statusListener) {
this.mOnPlayerStatusListener = statusListener;
}
//当音乐状态更新时进行回调,更新UI.
public void pause() {
if (isPlaying()) {
try {
mMediaPlayer.pause();
} catch (IllegalStateException e) {
LogUtils.d(TAG, "pause() an Error has occur..." + e.getMessage());
}
}
if (mOnPlayerStatusListener != null) {
mOnPlayerStatusListener.onPause();
}
}
3:接口定义
public interface IMediaPlayerStatusListener {
voidonPlay();
voidonPause();
}